Hello. I am learning C++. Could anybody please let me know what the following code means? CMotion is a class name. Not sure if FrameArray is a class or a function name. Thanks.
CMotion::CMotion(void) : FrameArray()
{
}
Printable View
Hello. I am learning C++. Could anybody please let me know what the following code means? CMotion is a class name. Not sure if FrameArray is a class or a function name. Thanks.
CMotion::CMotion(void) : FrameArray()
{
}
FrameArray will be a member of the CMotion class. That simply means that the member should be default-constructed. Since that's what would normally happen anyway, putting it there is redundant, but doesn't hurt anything.
If would be meaningful if FrameArray's class didn't have a default-constructor, so you had do do something like
in order to compile.Code:CMotion::CMotion(void) : FrameArray(0)
It can be a defaut initialization of a member variable. It can be an explicit call of the base class default constructor also.Quote:
I am learning C++. Could anybody please let me know what the following code means?
Thank you for your reply. Do you mean FrameArray is a "member function" of the CMotion class?
I just checked the corresponding .h file (written by others) and found the following:
class CMotion : private FrameArray, public CTManager
It seems that class CMotion is inherited from class CTManager and (class? function?) FrameArray. I am confused...
That's why you post all of the relevant code. FrameArray() could have been a member variable, a parent class, or a macro that does something.
Then obviously, FrameArray is a parent class.Quote:
I just checked the corresponding .h file (written by others) and found the following:
class CMotion : private FrameArray, public CTManager
Regards,
Paul McKenzie
C++ supports Multiple inheritence.
The above syntax says that CMotion is a class derived from FrameArray with private acess and from CTManager with public access.Code:
class CMotion : private FrameArray, public CTManager
Strictly speaking, I believe it is considered value initialisation rather than default initialisation, though there would only be a possibility of a difference in effect if FrameArray has no user declared constructors.Quote:
Originally Posted by ixSci
So, could you please tell me what the constructor in the original post do/mean? It seems that it does nothing. I guess what confuses me is that there is a () after the base class name FrameArray. Sorry I did not post relevant code at the beginning to cause the confusion. I found the following in related files:
typedef vector<CFrame> FrameArray
and
class CFrame
{
private:
Mat3x3d mat_data;
Vec3d vec_data;
:
:
}
Whoa. Is someone trying to derive from std::vector? Isn't that a no-no?
As with most things, it depends.
Generally speaking, you should avoid (publicly) deriving from anything that doesn't have a virtual destructor because you risk running into undefined behavior if someone tries to use your type in a polymorphic fashion.
Private inheritance (in combination with appropriate using-directives), on the other hand, will allow you to extend the functionality of STL facilities without having to write wrapper functions as would be required with composition. Assuming that you don't need to modify their behavior, that is.
Hmm, good to know.
Are you just randomly cherry-picking C++ syntax to look at, or are you learning C++ in a systematic, formal way? You cannot learn C++ by just looking at random code, and asking "what does this do"?
Are you using C++ books? If so, what does the book say about the member initialization list (which is what this entire discussion is about)?
Regards,
Paul McKenzie
I am very sorry. I try to modify a complex program written by former students. I have C++ How to Program and C++ Primer Plus. As far as I know, the member initialization list is for initializing the data members (i.e. variables) only. In this case, FrameArray is one of the base classes of CMotion, not a variable. So, I don't know that the code in the original post means. Do you mean we can initialize class also by putting it in the member initialization list?
You can do a partial initialization by calling one of the base class's constructors, yes. Again, since the default constructor would have been called anyway, I don't think having it there actually helps any.
Thank you for your answers.