[SOLVED] C++ : Constructor in 'Private' section of class
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
The public constructor private_cons is creating his own object, obj1 which assigns the value of 30 to i to that object. Once the constructor exists, that object goes out of scope and your 30 is gone. The i for obj is never assigned.
Add this line to the end of your public constructor and maybe it will make sense to you.
Do you have previous experience in some language, such as Java, that lets a constructor call another constructor for the same object?
C++ doesn't give you any way to do that. That fact is often inconvenient when writing C++ code.
As crabboy explained, what you actually did was have a constructor call a different constructor of the same class but for a different object.
One common, but imperfect, work around for not being able to call another constructor for the same object is to move all the actual construction to init methods:
Code:
#include <iostream>
using namespace std;
class private_cons
{
private:
int i;
private_cons(int num)
{
init(num);
}
void init(int num)
{
i = num;
}
public:
private_cons()
{
init(30);
}
int get_i()
{
return i;
}
};
int main ()
{
private_cons obj;
cout << "Value = " << obj.get_i();
return 0;
}
Another method, that is often better, is to move the data members and true constructors of your class into a base class:
Code:
#include <iostream>
using namespace std;
class base_class_for_private_cons
{
protected:
int i;
base_class_for_private_cons(int num) :
i(num)
{}
};
class private_cons : private base_class_for_private_cons
{
private:
private_cons(int num) :
base_class_for_private_cons(num)
{}
public:
private_cons() :
base_class_for_private_cons(30)
{}
int get_i()
{
return i;
}
};
int main ()
{
private_cons obj;
cout << "Value = " << obj.get_i();
return 0;
}
Depending on details of what else you want to do, the part I put in red is probably not needed.
You are violating basic concepts of the language. You are using the desired constructor on a different instance of the object (as the OP did) and that would be OK, but
You are using operator=() to copy to an incompletely constructed left hand side. That is wrong.
operator=() should be used only to replace contents of a fully constructed left hand side. It should not be used with a partially constructed left hand side.
We are looking at an example that is both trivial and POD. So you could do many things that are generally wrong C++ and they won't malfunction. But you should almost always code C++ as if you are working on a real project that will grow and evolve. Wrong code that happens to be safe because something else is unusually simple represents a land mine that is likely to blow up when unrelated reasonable looking changes are made.
Also, remember the OP didn't post the code He/She actually wants to use. We constantly tell people to cut their code down to a full compilable example that still contains the problem. That almost always involves keeping just the problem itself while discarding the reason it was coded that way. So when we make a suggestion for fixing the problem we should try not to assume the code was written that way for no good purpose.
If there is a good reason to have a private constructor and to want a public constructor to use the private constructor (rather than to simply initialize the members itself) then there is almost certainly also a reason that operator=() into a half constructed left hand side will malfunction.
You are using operator=() to copy to an incompletely constructed left hand side.
How is it incompletely constructed? Please quote chapter and verse when you make such a statement.
If there were any other members that needed to be initialized specifically, they could be added to the private constructor. As long as the right hand side is completely constructed, and all you want is a copy of the right hand side, I fail to see how the code is "wrong".
And, as he also pointed out, the bug is *especially* pernicious because most of the time (including in "toy" examples like this), it will *seem* to work.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.