开发者

C++ syntax question

I'm very new to c++, please help me to figure out how these operators work

class MyCalss : public State // MyClass inherits State?
{
private
MyClass(){} // Constructor for MyClass?
MyClass(const MyClass&);  // const means that invoking object will be not changed? What the meaning o开发者_如何学Gof '&' symbol? MyClass&

MyClass& operator = (const MyClass&) // What this statement exactly do? is it some kind operation overloading?

}

For example I have constructor

MyClass2::MyClass2(int i):BaseEntity(id),
m_iTotal(5),
m_iMonye(5),
m_iLevel(10){}

What is :BaseEntity(id) here means?

and can I rewrite it like this?

MyClass2::MyClass2(int i):BaseEntity(id)
{
m_iTotal = 5;
m_iMonye = 5;
m_iLevel = 10;
}


1) Yes MyClass inherits State. The public means that all its public functions will still be public in the derived class.
2) Yes thats a constructor.
3) Const DOES mean the object won't be changed. The "&" means you are passing in a reference to the object. This means you don't copy the whole object across the stack. It simply says this is where the object it is (ie refers to it). This saves some stack space and copying time.
4) operator = is an operator overload. It means that you can do the following:

MyClass a;
MyClass b;

// Populate and b.
a = b; // Overwrite a with b.

5) The BaseEntity( id ) means that the constructor calls the base class, BaseEntity's, constructor. id in this case is presumably a global or static class member otherwise the code won't compile.
6) You "could" re-write it like that but generally its better to do the former.


Ok, taking your questions one at a time:

class MyClass : public State // MyClass inherits State?

exactly so. MyClass inherits (publicly, which is usually the only kind of inheritance you care about) from State. Literally, MyClass "is a" State - everything true about State is also True about MyClass.

private
MyClass(){} // Constructor for MyClass?

Yes, MyClass(){} is a constructor for MyClass, specifically in this case taking no arguments and doing nothing (nothing special - memory will still be allocated, etc). However, because you've made MyClass() private, nothing else can call it, meaning you can't ever create a MyClass object (with no parameters) except from within MyClass methods.

MyClass(const MyClass&);  // const means that invoking object will be not changed? What the meaning of '&' symbol? MyClass&

Yes, const means that the object passed as a parameter won't be changed. This is, in fact, the Copy Constructor - the special constructor used to create a MyClass as a clone of an existing object. Again, it's private, so most of the world can't call it. The & indicates passing the parameter by reference - this prevents the compiler having to copy the object, and is often a good thing.

MyClass& operator = (const MyClass&) // What this statement exactly do? is it some kind operation overloading?

This is the copy-assignment operator, used to allow statements like `instance_a = instance_b;".

Note that, unless you define them yourself, the compiler will generate default versions of all of these three for you.

MyClass2::MyClass2(int i):BaseEntity(id),
m_iTotal(5),
m_iMonye(5),
m_iLevel(10){}

BaseEntity(id) is very similar to the other three lines; it's telling the compiler that when MyClass2 is constructed, it should call the parent class BaseEntity's constructor with the parameter id. Your second form of this constructor is very similar to your first form, but the first one - using an initialisation list - is considered better form.


BaseEntity is just another of the list of initializers. You could indeed rewrite the code as suggested and achieve nearly the same result. Please see:

http://www.cprogramming.com/tutorial/initialization-lists-c++.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜