|
-
January 26th, 2005, 11:31 AM
#1
A constructor calls another constructor of the same class?
class Foo {
public:
Foo(char x);
Foo(char x, int y);
...
};
Foo::Foo(char x)
{
...
Foo(x, 0); // this line does NOT help initialize the this object!!
...
}
My questions is why Foo(x,0) doesn't help initialize the this object? Thanks for your suggestions.
-
January 26th, 2005, 11:34 AM
#2
Re: A constructor calls another constructor of the same class?
 Originally Posted by dullboy
Foo::Foo(char x)
{
...
Foo(x, 0); // this line does NOT help initialize the this object!!
...
}
My questions is why Foo(x,0) doesn't help initialize the this object? Thanks for your suggestions.
Because Foo(x,0) is a brand-new temporary object. It has no influence on the current object.
Regards,
Paul McKenzie
-
January 26th, 2005, 11:40 AM
#3
Re: A constructor calls another constructor of the same class?
Code:
class foo
{
char m_x;
public:
foo(char x = 0); // 0 is the default value for x
void print();
}
foo::foo(char x) : m_x(x) // m_x = x
{
}
void foo:print()
{
cout << m_x << endl;
}
In this case you can create an object
Code:
foo f1;
f1.print(); // prints 0
foo f2(2);
f2.print(); // prints 2
And please use the C++ (Non VisualC++ Issues) Forum for non visual questions.
Last edited by cilu; January 26th, 2005 at 11:42 AM.
-
January 26th, 2005, 11:50 AM
#4
Re: A constructor calls another constructor of the same class?
Thanks for your reply. Perhaps my question should be that how a constructor of a class constructs "this" object?
 Originally Posted by Paul McKenzie
Because Foo(x,0) is a brand-new temporary object. It has no influence on the current object.
Regards,
Paul McKenzie
-
January 26th, 2005, 11:59 AM
#5
Re: A constructor calls another constructor of the same class?
this is a pointer accessible only within the nonstatic member functions of a class, struct, or union type. It points to the object for which the member function is called. Static member functions do not have a this pointer. An object's this pointer is not part of the object itself; it is not reflected in the result of a sizeof statement on the object. Instead, when a nonstatic member function is called for an object, the address of the object is passed by the compiler as a hidden argument to the function.
-
January 26th, 2005, 11:59 AM
#6
Re: A constructor calls another constructor of the same class?
Thanks for your reply. But I can't see your answer is related to my question. Please advise. I think often people here are more helpful. That is why I prefer posting my questions here.
 Originally Posted by cilu
Code:
class foo
{
char m_x;
public:
foo(char x = 0); // 0 is the default value for x
void print();
}
foo::foo(char x) : m_x(x) // m_x = x
{
}
void foo:print()
{
cout << m_x << endl;
}
In this case you can create an object
Code:
foo f1;
f1.print(); // prints 0
foo f2(2);
f2.print(); // prints 2
And please use the C++ (Non VisualC++ Issues) Forum for non visual questions.
-
January 26th, 2005, 12:04 PM
#7
Re: A constructor calls another constructor of the same class?
Actually, what I am concerned about at this point is how "this" is initialized in the constructor.
 Originally Posted by cilu
this is a pointer accessible only within the nonstatic member functions of a class, struct, or union type. It points to the object for which the member function is called. Static member functions do not have a this pointer. An object's this pointer is not part of the object itself; it is not reflected in the result of a sizeof statement on the object. Instead, when a nonstatic member function is called for an object, the address of the object is passed by the compiler as a hidden argument to the function.
-
January 26th, 2005, 12:25 PM
#8
Re: A constructor calls another constructor of the same class?
 Originally Posted by dullboy
Actually, what I am concerned about at this point is how "this" is initialized in the constructor.
I don't understand your question, or your concern for that matter. In the body of the constructor, this is valid.
I don't know what else to tell you. The compiler does what it does. Just follow the rules -- you're constructing a new object in C++ when you invoke the constructor, regardless of where you may be constructing the new object. The fact that you are constructing a Foo() object in the Foo() constructor is just a matter of consequence -- nothing special or extraordinary is occuring.
Also, this question should be posted in the non-Visual C++ section. There is just as much help there (maybe more so), when it comes to general C++ questions.
Regards,
Paul McKenzie
-
January 26th, 2005, 12:37 PM
#9
Re: A constructor calls another constructor of the same class?
Perhaps my question sounds stupid --- How can I EXPLICITLY initialize "this" in a constructor?
 Originally Posted by Paul McKenzie
I don't understand your question, or your concern for that matter. In the body of the constructor, this is valid.
I don't know what else to tell you. The compiler does what it does. Just follow the rules -- you're constructing a new object in C++ when you invoke the constructor, regardless of where you may be constructing the new object. The fact that you are constructing a Foo() object in the Foo() constructor is just a matter of consequence -- nothing special or extraordinary is occuring.
Also, this question should be posted in the non-Visual C++ section. There is just as much help there (maybe more so), when it comes to general C++ questions.
Regards,
Paul McKenzie
-
January 26th, 2005, 01:16 PM
#10
Re: A constructor calls another constructor of the same class?
 Originally Posted by dullboy
Perhaps my question sounds stupid --- How can I EXPLICITLY initialize "this" in a constructor?
From what it looks like you're trying to do, why not just have another function that does what you want, and call it from the constructor? Maybe call it Init() or something like that.
Code:
class Foo
{
public:
Foo(char x)
{
Init(x, 0);
}
Foo(char x, int y)
{
Init(x, y);
}
private:
void Init(char x, int y)
{
// whatever
}
};
Regards,
Paul McKenzie
-
January 26th, 2005, 02:16 PM
#11
Re: A constructor calls another constructor of the same class?
Follow Paul McKenzies' suggestion.
In C++ you cannot call another constructor for the same object from within that object.
-
January 26th, 2005, 03:57 PM
#12
Re: A constructor calls another constructor of the same class?
-
January 26th, 2005, 05:54 PM
#13
Re: A constructor calls another constructor of the same class?
What about this approach?
Code:
class Foo
{
public:
Foo(char x, char y = 0);
virtual ~Foo();
char m_x;
char m_y;
};
Foo::Foo(char x, char y/* = 0*/)
: m_x(x)
, m_y(y)
{
}
Foo::~Foo()
{
}
There are only 10 types of people in the world:
Those who understand binary and those who do not.
-
January 27th, 2005, 03:57 AM
#14
Re: A constructor calls another constructor of the same class?
 Originally Posted by dullboy
Perhaps my question sounds stupid --- How can I EXPLICITLY initialize "this" in a constructor?
The way I see it, is that you don't know what "this" is. I have to repeat myself. this is not another member object of the class. this is a const pointer, which is the address of the object whose method is called. When you call the constructor (as well as any other method) the address of the object is passed as a hidden parameter. And you can use this pointer to refer to the object itself. So you cannot speak about a special initialization of this pointer, rather than the object. You must see this as an alter ego of the object.
-
January 27th, 2005, 04:49 AM
#15
Re: A constructor calls another constructor of the same class?
 Originally Posted by dullboy
Perhaps my question sounds stupid --- How can I EXPLICITLY initialize "this" in a constructor?
Possibly the reason for the confusion is in that statement. Do you mean that you want to set the value of this? I.e. that you want to set the address of the object? Or that you want to explicitly initialise *this - i.e. the member variables of the object pointed to?
For the former, the only way to do that is to use placement new - i.e. allocate some storage and construct the object there.
For the latter: well, that's exactly what the constructor is doing, and this is where the confusion may be creeping in - you seem to be asking how to do something that you are already doing. The suggestions given so far should help.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
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
|