|
-
April 20th, 2009, 11:19 PM
#1
What does this constructor definition mean?
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()
{
}
-
April 20th, 2009, 11:28 PM
#2
Re: What does this constructor definition mean?
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
Code:
CMotion::CMotion(void) : FrameArray(0)
in order to compile.
-
April 20th, 2009, 11:47 PM
#3
Re: What does this constructor definition mean?
I am learning C++. Could anybody please let me know what the following code means?
It can be a defaut initialization of a member variable. It can be an explicit call of the base class default constructor also.
-
April 20th, 2009, 11:55 PM
#4
Re: What does this constructor definition mean?
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...
-
April 21st, 2009, 12:09 AM
#5
Re: What does this constructor definition mean?
 Originally Posted by hajimeml
Thank you for your reply. Do you mean FrameArray is a "member function" of the CMotion class?
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.
I just checked the corresponding .h file (written by others) and found the following:
class CMotion : private FrameArray, public CTManager
Then obviously, FrameArray is a parent class.
Regards,
Paul McKenzie
-
April 21st, 2009, 12:15 AM
#6
Re: What does this constructor definition mean?
C++ supports Multiple inheritence.
Code:
class CMotion : private FrameArray, public CTManager
The above syntax says that CMotion is a class derived from FrameArray with private acess and from CTManager with public access.
-
April 21st, 2009, 12:23 AM
#7
Re: What does this constructor definition mean?
 Originally Posted by ixSci
It can be a defaut initialization of a member variable.
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.
-
April 21st, 2009, 12:38 AM
#8
Re: What does this constructor definition mean?
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;
:
:
}
-
April 21st, 2009, 12:42 AM
#9
Re: What does this constructor definition mean?
Whoa. Is someone trying to derive from std::vector? Isn't that a no-no?
-
April 21st, 2009, 01:05 AM
#10
Re: What does this constructor definition mean?
 Originally Posted by Lindley
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.
Last edited by Plasmator; April 21st, 2009 at 01:07 AM.
-
April 21st, 2009, 09:11 AM
#11
Re: What does this constructor definition mean?
-
April 21st, 2009, 08:01 PM
#12
Re: What does this constructor definition mean?
 Originally Posted by Lindley
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
Code:
CMotion::CMotion(void) : FrameArray(0)
in order to compile.
So, what does the () after the class name FrameArray mean?
As there is nothing in between {}, can I assume that the code in the original post does nothing?
-
April 21st, 2009, 08:22 PM
#13
Re: What does this constructor definition mean?
 Originally Posted by hajimeml
So, what does the () after the class name FrameArray mean?
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
-
April 21st, 2009, 09:17 PM
#14
Re: What does this constructor definition mean?
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?
-
April 21st, 2009, 09:26 PM
#15
Re: What does this constructor definition mean?
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|