#include <iostream>
#define n 255
using namespace std;
int main()
{
int i=n;
int *ptr=&i;
int const *ptr_1=&i;
const int *ptr_2=&i;
const int * const ptr_3=&i;
}
Run Code Online (Sandbox Code Playgroud)
为什么这个代码在Visual C++,Dev C++和G ++中编译?链接到- Ideone -
请告诉我从c ++开始执行的地方,希望你的回答是"来自主要".
那怎么样?
class abc
{
public:
abc()
{
cout<<"hello";
}
};
const abc obj;
int main( )
{
cout<<"Main";
}
Run Code Online (Sandbox Code Playgroud)
输出:
helloMain
Run Code Online (Sandbox Code Playgroud)
请详细说明.
我对虚拟表的功能并不太了解,但是在下面粘贴的代码中 - this
传递的指针明显指向2种情况下的不同位置 - 但是show()
内存中的函数- 是实例化/意味着单独创建的运行时的每个对象?(原谅我对C++术语的不了解)
#include<iostream>
using namespace std;
class A
{
int x;
public:
A(){x=0;}
A(int z){x=z;}
void show()
{
if(x==0)
cout<<"\nCalled by OBJ_1";
else
cout << "\nCalled by OBJ_2";
}
};
int main()
{
A OBJ_1,OBJ_2(1);
OBJ_1.show();
OBJ_2.show();
}
Run Code Online (Sandbox Code Playgroud)
如果可以提供关于虚拟表如何工作的示例(如果可能的话有一些存储器图)并且this
可以解释虚拟表的指针功能,我将非常感激.
如果我尝试初始化obj_s
它要求我做它const
- 我不能这样做因为我必须继续计算我的创建对象.
#include<iostream>
class A
{
static int obj_s=0;
public:
A(){ ++obj_s;cout << A::obj_s << "\nObject(s) Created\n"; }
};
int main()
{
A a,b,c,d;
}
Run Code Online (Sandbox Code Playgroud)
下面的代码一直给我以下错误:
[Linker error] undefined reference to `A::obj_s'
Run Code Online (Sandbox Code Playgroud) #include<iostream>
class _ctor
{
public:
_ctor() { std::cout<<"\nCtor";}
~_ctor(){ std::cout<<"\nDtor";}
};
_ctor A(); // --> Is the Constructor Really called? I do not see the Output printed
//_ctor A;
int main(){
return 0;
}
Run Code Online (Sandbox Code Playgroud)
上面代码的输出在这个链接中给出
我没有看到构造函数被调用,可能是什么问题?如果它不应该被调用那么这意味着_ctor A();
什么?
void catchlabel()
{
if(vecs.empty())
return;
else
{
cout << "The Sizeof the Vector is: " << vecs.size() << endl;
cout << "Currently Stored Labels: " << endl;
/* Error 1 */
for ( int i = 1, vector<string>::iterator it = vecs.begin(); it != vecs.end(); ++it , i++)
cout << i << ". "<< *it << endl;
cout << endl;
}
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
1>错误C2146:语法错误:在标识符'it'之前缺少','
如何解决这个问题?
在Stanley B. Lippman*的书C++入门书中,*JoséeLajoie
在类构造函数的第14.2章中,它指出:
我们是否还应该为指定期初余额但没有客户名称提供支持?碰巧,类规范明确禁止这样做.我们的带有默认第二个参数的双参数构造函数提供了一个完整的接口,用于接受可由用户设置的类Account的数据成员的初始值:
class Account {
public:
// default constructor ...
Account();
// parameter names are not necessary in declaration
Account( const char*, double=0.0 );
const char* name() { return _name; } // What is this for??
// ...
private:
// ...
};
Run Code Online (Sandbox Code Playgroud)
以下是合法的Account类对象定义,将一个或两个参数传递给构造函数:
int main()
{
// ok: both invoke two-parameter constructor
Account acct( "Ethan Stern" );
Run Code Online (Sandbox Code Playgroud)
当没有使用单个参数声明时,如何调用2参数构造函数?
Account *pact = new Account( "Michael Lieberman", 5000 );
Run Code Online (Sandbox Code Playgroud)
以上行如何使用默认参数调用构造函数
if ( strcmp( acct.name(), pact->name() )) …
Run Code Online (Sandbox Code Playgroud) 鉴于代码:
#include<iostream>
using namespace std;
class String
{
char *pstr;
unsigned size;
public:
String(){ pstr=0;size=0;}
String(const char *);
void show(){ cout << pstr << endl ; }
~String () { cout << "In Dtor" << endl; delete [] pstr; }
};
String::String(const char * cptr)
{
size = strlen (cptr) + 1;
cout << "String is - " << cptr << " - of size " << size - 1 << endl ;
pstr = new char [ size ] …
Run Code Online (Sandbox Code Playgroud) #include<stdio.h>
int main(void)
{
int a=-3,b=5,c;
c=a|b;
printf("%d ",c);
c=a&b;
printf("%d ",c);
}
Run Code Online (Sandbox Code Playgroud)
输出是-3 5
,请解释一下如何?
c++ ×9
visual-c++ ×5
c++11 ×4
c ×1
const ×1
destructor ×1
function ×1
managed-c++ ×1
string ×1