tags:

views:

106

answers:

4

Hello, 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 of '&' 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;
}
A: 

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

Tom Slick
A: 
class MyCalss : public State -> MyClass(derived class) inherited State(base class)

MyClass(){} -> Default constructor for your class, compiler generates the default one
              if you do not specify any constructor

Ampersand (&)-> reference operator(C++ uses this a lot, so pay proper attention to this   
operator) 

MyClass& operator = (const MyClass&) -> Assignment operator for your class, not inherited
from the State class and have to call the State assignment to do proper assignment of 
derived, as they contain base parts also, objects.    

:BaseEntity(id) -> use of initialization list. It is preferred over the second version of yours

But most important get a good C++ book. These all and more would be explained in the first 5 chapters itself.

DumbCoder
+2  A: 

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.

Goz
A: 

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.

Chris
Thank you, your comments helped me a lot!
in my case MyClass is singleton, thats why constructor is private
I thought it might be, but that seemed beyond the scope of this question (given how long my answer was anyway!) Glad to be of help.
Chris