static . , .
, , , .
, .
.
- .
- , :
□
□ , , sizeof.
class A{
public:
static int cout;
};
int A::cout;
A b,*c;
int main()
{
cout<<A::cout<<endl;//10
cout<<b.cout<<endl;//10
cout<<c->cout<<endl;//10
A::cout=100;
cout<<A::cout<<endl;//10
cout<<b.cout<<endl;//10
cout<<c->cout<<endl;//10
/////////////////////////////
cin.get();
return 0;
}
//---------------------------------------------------------------------------
. , this. , , , , .
#include <iostream>
using namespace std;
class B{
static int cout;
int data;
public:
static void inc_cout(){cout++;}
static int get_cout(){return cout;}
};
int B::cout(13);
int main()
{
B d,*e;
cout<<d.get_cout()<<endl;
d.inc_cout();
cout<<e->get_cout()<<endl;
cin.get();
return 0;
}
//---------------------------------------------------------------------------
(const) (virtual).
, . .
.
.
- , , friend.
- , this .
|
|
- .
- .
- .
- , friend.
#include <iostream>
using namespace std;
class TA{
int a;
int b;
public:
int d;
TA (int x=3,int y=4){a=x;b=y;}
friend void print(TA&);
friend class TB;
};
class TB{
public:
void inc_TA(TA& inca){inca.a++;};
void inc_TB(TA& incb){incb.b++;};
};
void print(TA &obj)
{cout<<obj.a<<"\t"<<obj.b<<endl;}
int main()
{
TA b,*c,d(4,5),*z;
c=new TA();
z=new TA(7,8);
print(b);
print(d); //4 5
print(*c);
print(*z);
delete c;
delete z;
TB q;
q.inc_TA(d);
q.inc_TB(d);
print(d);//5 6
cin.get();
return 0;
}
friend .
, , . , :
□ , ;
□ main:
□ , , delete.
(-), . :
□ ;
□ const static;
□ ;
.
, .
, , , -, .
.
:
, new . .
C++ , .
, C++, :
..*?::: # ## sizeof
(-) :
□ , ( ), ;
□ ;
□ - ;
□ - ( -);
□ - static.
|
|
- : , , .
, , 1.
- operator, :
operator ( ) { }