How to call the appropriate constructorHow do you set, clear, and toggle a single bit?What are the rules for calling the superclass constructor?How do I iterate over the words of a string?Can I call a constructor from another constructor (do constructor chaining) in C++?How can I profile C++ code running on Linux?C++11 introduced a standardized memory model. What does it mean? And how is it going to affect C++ programming?Array of Object - std::array - Constructor Initialization ProblemsAmbiguous call: int to double or boolInitializer list as container does not workskipping adding constructors when inheriting from std::string class
Explain the ending of Black Mirror's "Smithereens"
How to hide rifle during medieval town entrance inspection?
New bike, tubeless tire will not inflate
Why does ''cat "$1:-/dev/stdin | ... &>/dev/null'' work in bash but not dash?
Why not invest in precious metals?
How can I end combat quickly when the outcome is inevitable?
Has there been a multiethnic Star Trek character?
Can the removal of a duty-free sales trolley result in a measurable reduction in emissions?
Does the new finding on "reversing a quantum jump mid-flight" rule out any interpretations of QM?
Why am I Seeing A Weird "Notch" on the Data Line For Some Logical 1s?
How can I make 12 tone and atonal melodies sound interesting?
With Ubuntu 18.04, how can I have a hot corner that locks the computer?
If there's something that implicates the president why is there then a national security issue? (John Dowd)
Generate basis elements of the Steenrod algebra
How creative should the DM let an artificer be in terms of what they can build?
What is the purpose of bonds within an investment portfolio?
Why are MBA programs closing?
Getting UPS Power from One Room to Another
How to trick the reader into thinking they're following a redshirt instead of the protagonist?
Is it possible to have 2 different but equal size real number sets that have the same mean and standard deviation?
Non-aqueous eyes?
Electricity free spaceship
Is there a DSLR/mirorless camera with minimal options like a classic, simple SLR?
A word that means "blending into a community too much"
How to call the appropriate constructor
How do you set, clear, and toggle a single bit?What are the rules for calling the superclass constructor?How do I iterate over the words of a string?Can I call a constructor from another constructor (do constructor chaining) in C++?How can I profile C++ code running on Linux?C++11 introduced a standardized memory model. What does it mean? And how is it going to affect C++ programming?Array of Object - std::array - Constructor Initialization ProblemsAmbiguous call: int to double or boolInitializer list as container does not workskipping adding constructors when inheriting from std::string class
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
Why am I not able to call the appropriate constructor for the Geometry object?
class Geometry
private:
float fRadius;
int iSegments;
float fWidth;
float fLenght;
std::string stdstrType;
bool bValid;
public:
Geometry()
// Set data Elements
qDebug() << "Constructor 1 is called";
Geometry(float Radius, int Segments, float Width, float Length,
std::string strType, bool bValue)
// Set data Elements
qDebug() << "Constructor 2 is called";
Geometry(const Geometry & g)
// Set data Elements
qDebug() << "Constructor 3 is called";
I use this class as a data variable in another class.
class Container
private:
std::string stdstrContainerName;
std::string stdstrPluginType;
Geometry Geom;
public:
Container();
Container(std::string, std::string, Geometry geometry);
;
Container::Container()
stdstrContainerName = "Group";
stdstrPluginType = "Geometry";
Container::Container(std::string strName, std::string strType,
Geometry geometry)
stdstrContainerName = stdstrContainerName;
stdstrPluginType = stdstrPluginType;
Geom = geometry;
When I try to set a Geometry object in the container, even though I have given all the parameters for constructor 2 to be called, constructor 1 is called.
geometry(0.3, 32, 0.0, 0.0, "SPHERE", true);
Container cont("Sphere", "SPHERE", geometry);
c++ c++11 visual-c++
add a comment |
Why am I not able to call the appropriate constructor for the Geometry object?
class Geometry
private:
float fRadius;
int iSegments;
float fWidth;
float fLenght;
std::string stdstrType;
bool bValid;
public:
Geometry()
// Set data Elements
qDebug() << "Constructor 1 is called";
Geometry(float Radius, int Segments, float Width, float Length,
std::string strType, bool bValue)
// Set data Elements
qDebug() << "Constructor 2 is called";
Geometry(const Geometry & g)
// Set data Elements
qDebug() << "Constructor 3 is called";
I use this class as a data variable in another class.
class Container
private:
std::string stdstrContainerName;
std::string stdstrPluginType;
Geometry Geom;
public:
Container();
Container(std::string, std::string, Geometry geometry);
;
Container::Container()
stdstrContainerName = "Group";
stdstrPluginType = "Geometry";
Container::Container(std::string strName, std::string strType,
Geometry geometry)
stdstrContainerName = stdstrContainerName;
stdstrPluginType = stdstrPluginType;
Geom = geometry;
When I try to set a Geometry object in the container, even though I have given all the parameters for constructor 2 to be called, constructor 1 is called.
geometry(0.3, 32, 0.0, 0.0, "SPHERE", true);
Container cont("Sphere", "SPHERE", geometry);
c++ c++11 visual-c++
add a comment |
Why am I not able to call the appropriate constructor for the Geometry object?
class Geometry
private:
float fRadius;
int iSegments;
float fWidth;
float fLenght;
std::string stdstrType;
bool bValid;
public:
Geometry()
// Set data Elements
qDebug() << "Constructor 1 is called";
Geometry(float Radius, int Segments, float Width, float Length,
std::string strType, bool bValue)
// Set data Elements
qDebug() << "Constructor 2 is called";
Geometry(const Geometry & g)
// Set data Elements
qDebug() << "Constructor 3 is called";
I use this class as a data variable in another class.
class Container
private:
std::string stdstrContainerName;
std::string stdstrPluginType;
Geometry Geom;
public:
Container();
Container(std::string, std::string, Geometry geometry);
;
Container::Container()
stdstrContainerName = "Group";
stdstrPluginType = "Geometry";
Container::Container(std::string strName, std::string strType,
Geometry geometry)
stdstrContainerName = stdstrContainerName;
stdstrPluginType = stdstrPluginType;
Geom = geometry;
When I try to set a Geometry object in the container, even though I have given all the parameters for constructor 2 to be called, constructor 1 is called.
geometry(0.3, 32, 0.0, 0.0, "SPHERE", true);
Container cont("Sphere", "SPHERE", geometry);
c++ c++11 visual-c++
Why am I not able to call the appropriate constructor for the Geometry object?
class Geometry
private:
float fRadius;
int iSegments;
float fWidth;
float fLenght;
std::string stdstrType;
bool bValid;
public:
Geometry()
// Set data Elements
qDebug() << "Constructor 1 is called";
Geometry(float Radius, int Segments, float Width, float Length,
std::string strType, bool bValue)
// Set data Elements
qDebug() << "Constructor 2 is called";
Geometry(const Geometry & g)
// Set data Elements
qDebug() << "Constructor 3 is called";
I use this class as a data variable in another class.
class Container
private:
std::string stdstrContainerName;
std::string stdstrPluginType;
Geometry Geom;
public:
Container();
Container(std::string, std::string, Geometry geometry);
;
Container::Container()
stdstrContainerName = "Group";
stdstrPluginType = "Geometry";
Container::Container(std::string strName, std::string strType,
Geometry geometry)
stdstrContainerName = stdstrContainerName;
stdstrPluginType = stdstrPluginType;
Geom = geometry;
When I try to set a Geometry object in the container, even though I have given all the parameters for constructor 2 to be called, constructor 1 is called.
geometry(0.3, 32, 0.0, 0.0, "SPHERE", true);
Container cont("Sphere", "SPHERE", geometry);
c++ c++11 visual-c++
c++ c++11 visual-c++
edited May 24 at 7:19
Stack Danny
2,423933
2,423933
asked May 24 at 5:52
shomitshomit
1098
1098
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
Regarding your use case, here's what each line calls:
Geometry geometry(0.3, 32, 0.0, 0.0, "SPHERE", true); // Geometry constructor 2
Container cont("Sphere", "SPHERE", geometry); // Container constructor 2, Geometry constructors 3 & 1
Here, the constructor for Geometry is actually called outside the constructor of Container. But Geometry constructor 3 and 1 are also being called... why?
Why indeed. Since the constructor for Container takes a Geometry parameter by value, the geometry object passed will be copied (hence, the copy constructor is called). Next, Geometry constructor 1, aka the default constructor is actually called in the constructor of Container. Afterwards, copy-assignment, another implicitly-generated special method, is called:
Container::Container(std::string strName, std::string strType, Geometry geometry)
/*: stdstrContainerName()
, stdstrPluginType()
, Geom()*/ // default-constructors implicitly called as member-initialisation
stdstrContainerName = stdstrContainerName;
stdstrPluginType = stdstrPluginType;
Geom = geometry; // copy-assignment, i.e. operator= (Geometry const&)
To override the default behaviour, use member initialisation explicitly:
Container::Container(std::string strName, std::string strType, Geometry geometry)
: stdstrContainerName(strName)
, stdstrPluginType(strType)
, Geom(geometry) // copy-constructor, i.e. Geometry(Geometry const&)
This should yield constructor 3, as the copy-constructor is now called.
Demo
On switching over to member initialisation, you may have noticed that constructor 3 is called twice. Again, this is due to the constructor of Container taking its geometry parameter by value, creating a new object via copy-constructing. To prevent a copy from being made and make the constructor more efficient, we can pass geometry by reference. In addition, we can const-ify the parameter to guarantee that the reference isn't modified in the constructor.
Thus the constructor of Container can be changed to:
Container(const std::string &strName, const std::string &strType, const Geometry &geometry);
As answered, in the first case the internal geometry object is already created by the time you reach the assignment line. So essentially the Geometry object is being constructed using no args. It is preferred to use initialization lists in case of such cases, you can read-up more about it here
– Bhavin
May 24 at 6:12
1
In the second block of code the comment is a bit misleading because you call the constructor explicitly there's no implicit thing happening. Besides that I agree with your answer.
– navyblue
May 24 at 6:16
add a comment |
The Container's constructor:
Container::Container(std::string strName, std::string strType, Geometry geometry)
stdstrContainerName = stdstrContainerName;
stdstrPluginType = stdstrPluginType;
Geom = geometry;
doesn't have any explicit initialization of the Geom field. It is first default initialized hence the default constructor call and then you assign it the geometry argument.
To achive what you want, you need to define Container's constructor this way:
Container::Container(std::string strName, std::string strType, Geometry geometry)
: Geom(geometry)
stdstrContainerName = strName;
stdstrPluginType = strType;
Notice the : Geom(geometry) part. This is where the constructor of Geom is called and if you don't put something like that in your constructor then the default one is called.
Also, I'm almost certain that you have a bug in your constructor. It probably should be stdstrContainerName = strName; and not stdstrContainerName = stdstrContainerName;. The same applies to stdstrPluginType.
One more thing, this is not a bug and is technically correct but passing objects of types like std::string or Geometry (that is objects which might be 'heavy') might be decrease performance so why don't you pass them by reference? But that's not a bug (at least not in the piece of code you posted) and isn't directly related to your question.
add a comment |
Constructor #1 is called for the Geometry geometry argument passed by value to the constructor of Container. Because you are passing it by value, it is recreated on the stack of the constructor of Container. Change it to const Geometry& geometry.
How can i change it to be passed for the appropriate constructor ?
– shomit
May 24 at 5:55
Passing by reference is a good suggestion but it doesn't solve the problem in the question and has actually nothing to do with calling the default constructor. For details see my answer to the question or the answer of TrebledJ.
– navyblue
May 24 at 6:13
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f56286563%2fhow-to-call-the-appropriate-constructor%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Regarding your use case, here's what each line calls:
Geometry geometry(0.3, 32, 0.0, 0.0, "SPHERE", true); // Geometry constructor 2
Container cont("Sphere", "SPHERE", geometry); // Container constructor 2, Geometry constructors 3 & 1
Here, the constructor for Geometry is actually called outside the constructor of Container. But Geometry constructor 3 and 1 are also being called... why?
Why indeed. Since the constructor for Container takes a Geometry parameter by value, the geometry object passed will be copied (hence, the copy constructor is called). Next, Geometry constructor 1, aka the default constructor is actually called in the constructor of Container. Afterwards, copy-assignment, another implicitly-generated special method, is called:
Container::Container(std::string strName, std::string strType, Geometry geometry)
/*: stdstrContainerName()
, stdstrPluginType()
, Geom()*/ // default-constructors implicitly called as member-initialisation
stdstrContainerName = stdstrContainerName;
stdstrPluginType = stdstrPluginType;
Geom = geometry; // copy-assignment, i.e. operator= (Geometry const&)
To override the default behaviour, use member initialisation explicitly:
Container::Container(std::string strName, std::string strType, Geometry geometry)
: stdstrContainerName(strName)
, stdstrPluginType(strType)
, Geom(geometry) // copy-constructor, i.e. Geometry(Geometry const&)
This should yield constructor 3, as the copy-constructor is now called.
Demo
On switching over to member initialisation, you may have noticed that constructor 3 is called twice. Again, this is due to the constructor of Container taking its geometry parameter by value, creating a new object via copy-constructing. To prevent a copy from being made and make the constructor more efficient, we can pass geometry by reference. In addition, we can const-ify the parameter to guarantee that the reference isn't modified in the constructor.
Thus the constructor of Container can be changed to:
Container(const std::string &strName, const std::string &strType, const Geometry &geometry);
As answered, in the first case the internal geometry object is already created by the time you reach the assignment line. So essentially the Geometry object is being constructed using no args. It is preferred to use initialization lists in case of such cases, you can read-up more about it here
– Bhavin
May 24 at 6:12
1
In the second block of code the comment is a bit misleading because you call the constructor explicitly there's no implicit thing happening. Besides that I agree with your answer.
– navyblue
May 24 at 6:16
add a comment |
Regarding your use case, here's what each line calls:
Geometry geometry(0.3, 32, 0.0, 0.0, "SPHERE", true); // Geometry constructor 2
Container cont("Sphere", "SPHERE", geometry); // Container constructor 2, Geometry constructors 3 & 1
Here, the constructor for Geometry is actually called outside the constructor of Container. But Geometry constructor 3 and 1 are also being called... why?
Why indeed. Since the constructor for Container takes a Geometry parameter by value, the geometry object passed will be copied (hence, the copy constructor is called). Next, Geometry constructor 1, aka the default constructor is actually called in the constructor of Container. Afterwards, copy-assignment, another implicitly-generated special method, is called:
Container::Container(std::string strName, std::string strType, Geometry geometry)
/*: stdstrContainerName()
, stdstrPluginType()
, Geom()*/ // default-constructors implicitly called as member-initialisation
stdstrContainerName = stdstrContainerName;
stdstrPluginType = stdstrPluginType;
Geom = geometry; // copy-assignment, i.e. operator= (Geometry const&)
To override the default behaviour, use member initialisation explicitly:
Container::Container(std::string strName, std::string strType, Geometry geometry)
: stdstrContainerName(strName)
, stdstrPluginType(strType)
, Geom(geometry) // copy-constructor, i.e. Geometry(Geometry const&)
This should yield constructor 3, as the copy-constructor is now called.
Demo
On switching over to member initialisation, you may have noticed that constructor 3 is called twice. Again, this is due to the constructor of Container taking its geometry parameter by value, creating a new object via copy-constructing. To prevent a copy from being made and make the constructor more efficient, we can pass geometry by reference. In addition, we can const-ify the parameter to guarantee that the reference isn't modified in the constructor.
Thus the constructor of Container can be changed to:
Container(const std::string &strName, const std::string &strType, const Geometry &geometry);
As answered, in the first case the internal geometry object is already created by the time you reach the assignment line. So essentially the Geometry object is being constructed using no args. It is preferred to use initialization lists in case of such cases, you can read-up more about it here
– Bhavin
May 24 at 6:12
1
In the second block of code the comment is a bit misleading because you call the constructor explicitly there's no implicit thing happening. Besides that I agree with your answer.
– navyblue
May 24 at 6:16
add a comment |
Regarding your use case, here's what each line calls:
Geometry geometry(0.3, 32, 0.0, 0.0, "SPHERE", true); // Geometry constructor 2
Container cont("Sphere", "SPHERE", geometry); // Container constructor 2, Geometry constructors 3 & 1
Here, the constructor for Geometry is actually called outside the constructor of Container. But Geometry constructor 3 and 1 are also being called... why?
Why indeed. Since the constructor for Container takes a Geometry parameter by value, the geometry object passed will be copied (hence, the copy constructor is called). Next, Geometry constructor 1, aka the default constructor is actually called in the constructor of Container. Afterwards, copy-assignment, another implicitly-generated special method, is called:
Container::Container(std::string strName, std::string strType, Geometry geometry)
/*: stdstrContainerName()
, stdstrPluginType()
, Geom()*/ // default-constructors implicitly called as member-initialisation
stdstrContainerName = stdstrContainerName;
stdstrPluginType = stdstrPluginType;
Geom = geometry; // copy-assignment, i.e. operator= (Geometry const&)
To override the default behaviour, use member initialisation explicitly:
Container::Container(std::string strName, std::string strType, Geometry geometry)
: stdstrContainerName(strName)
, stdstrPluginType(strType)
, Geom(geometry) // copy-constructor, i.e. Geometry(Geometry const&)
This should yield constructor 3, as the copy-constructor is now called.
Demo
On switching over to member initialisation, you may have noticed that constructor 3 is called twice. Again, this is due to the constructor of Container taking its geometry parameter by value, creating a new object via copy-constructing. To prevent a copy from being made and make the constructor more efficient, we can pass geometry by reference. In addition, we can const-ify the parameter to guarantee that the reference isn't modified in the constructor.
Thus the constructor of Container can be changed to:
Container(const std::string &strName, const std::string &strType, const Geometry &geometry);
Regarding your use case, here's what each line calls:
Geometry geometry(0.3, 32, 0.0, 0.0, "SPHERE", true); // Geometry constructor 2
Container cont("Sphere", "SPHERE", geometry); // Container constructor 2, Geometry constructors 3 & 1
Here, the constructor for Geometry is actually called outside the constructor of Container. But Geometry constructor 3 and 1 are also being called... why?
Why indeed. Since the constructor for Container takes a Geometry parameter by value, the geometry object passed will be copied (hence, the copy constructor is called). Next, Geometry constructor 1, aka the default constructor is actually called in the constructor of Container. Afterwards, copy-assignment, another implicitly-generated special method, is called:
Container::Container(std::string strName, std::string strType, Geometry geometry)
/*: stdstrContainerName()
, stdstrPluginType()
, Geom()*/ // default-constructors implicitly called as member-initialisation
stdstrContainerName = stdstrContainerName;
stdstrPluginType = stdstrPluginType;
Geom = geometry; // copy-assignment, i.e. operator= (Geometry const&)
To override the default behaviour, use member initialisation explicitly:
Container::Container(std::string strName, std::string strType, Geometry geometry)
: stdstrContainerName(strName)
, stdstrPluginType(strType)
, Geom(geometry) // copy-constructor, i.e. Geometry(Geometry const&)
This should yield constructor 3, as the copy-constructor is now called.
Demo
On switching over to member initialisation, you may have noticed that constructor 3 is called twice. Again, this is due to the constructor of Container taking its geometry parameter by value, creating a new object via copy-constructing. To prevent a copy from being made and make the constructor more efficient, we can pass geometry by reference. In addition, we can const-ify the parameter to guarantee that the reference isn't modified in the constructor.
Thus the constructor of Container can be changed to:
Container(const std::string &strName, const std::string &strType, const Geometry &geometry);
edited May 25 at 8:11
answered May 24 at 6:01
TrebledJTrebledJ
4,84541434
4,84541434
As answered, in the first case the internal geometry object is already created by the time you reach the assignment line. So essentially the Geometry object is being constructed using no args. It is preferred to use initialization lists in case of such cases, you can read-up more about it here
– Bhavin
May 24 at 6:12
1
In the second block of code the comment is a bit misleading because you call the constructor explicitly there's no implicit thing happening. Besides that I agree with your answer.
– navyblue
May 24 at 6:16
add a comment |
As answered, in the first case the internal geometry object is already created by the time you reach the assignment line. So essentially the Geometry object is being constructed using no args. It is preferred to use initialization lists in case of such cases, you can read-up more about it here
– Bhavin
May 24 at 6:12
1
In the second block of code the comment is a bit misleading because you call the constructor explicitly there's no implicit thing happening. Besides that I agree with your answer.
– navyblue
May 24 at 6:16
As answered, in the first case the internal geometry object is already created by the time you reach the assignment line. So essentially the Geometry object is being constructed using no args. It is preferred to use initialization lists in case of such cases, you can read-up more about it here
– Bhavin
May 24 at 6:12
As answered, in the first case the internal geometry object is already created by the time you reach the assignment line. So essentially the Geometry object is being constructed using no args. It is preferred to use initialization lists in case of such cases, you can read-up more about it here
– Bhavin
May 24 at 6:12
1
1
In the second block of code the comment is a bit misleading because you call the constructor explicitly there's no implicit thing happening. Besides that I agree with your answer.
– navyblue
May 24 at 6:16
In the second block of code the comment is a bit misleading because you call the constructor explicitly there's no implicit thing happening. Besides that I agree with your answer.
– navyblue
May 24 at 6:16
add a comment |
The Container's constructor:
Container::Container(std::string strName, std::string strType, Geometry geometry)
stdstrContainerName = stdstrContainerName;
stdstrPluginType = stdstrPluginType;
Geom = geometry;
doesn't have any explicit initialization of the Geom field. It is first default initialized hence the default constructor call and then you assign it the geometry argument.
To achive what you want, you need to define Container's constructor this way:
Container::Container(std::string strName, std::string strType, Geometry geometry)
: Geom(geometry)
stdstrContainerName = strName;
stdstrPluginType = strType;
Notice the : Geom(geometry) part. This is where the constructor of Geom is called and if you don't put something like that in your constructor then the default one is called.
Also, I'm almost certain that you have a bug in your constructor. It probably should be stdstrContainerName = strName; and not stdstrContainerName = stdstrContainerName;. The same applies to stdstrPluginType.
One more thing, this is not a bug and is technically correct but passing objects of types like std::string or Geometry (that is objects which might be 'heavy') might be decrease performance so why don't you pass them by reference? But that's not a bug (at least not in the piece of code you posted) and isn't directly related to your question.
add a comment |
The Container's constructor:
Container::Container(std::string strName, std::string strType, Geometry geometry)
stdstrContainerName = stdstrContainerName;
stdstrPluginType = stdstrPluginType;
Geom = geometry;
doesn't have any explicit initialization of the Geom field. It is first default initialized hence the default constructor call and then you assign it the geometry argument.
To achive what you want, you need to define Container's constructor this way:
Container::Container(std::string strName, std::string strType, Geometry geometry)
: Geom(geometry)
stdstrContainerName = strName;
stdstrPluginType = strType;
Notice the : Geom(geometry) part. This is where the constructor of Geom is called and if you don't put something like that in your constructor then the default one is called.
Also, I'm almost certain that you have a bug in your constructor. It probably should be stdstrContainerName = strName; and not stdstrContainerName = stdstrContainerName;. The same applies to stdstrPluginType.
One more thing, this is not a bug and is technically correct but passing objects of types like std::string or Geometry (that is objects which might be 'heavy') might be decrease performance so why don't you pass them by reference? But that's not a bug (at least not in the piece of code you posted) and isn't directly related to your question.
add a comment |
The Container's constructor:
Container::Container(std::string strName, std::string strType, Geometry geometry)
stdstrContainerName = stdstrContainerName;
stdstrPluginType = stdstrPluginType;
Geom = geometry;
doesn't have any explicit initialization of the Geom field. It is first default initialized hence the default constructor call and then you assign it the geometry argument.
To achive what you want, you need to define Container's constructor this way:
Container::Container(std::string strName, std::string strType, Geometry geometry)
: Geom(geometry)
stdstrContainerName = strName;
stdstrPluginType = strType;
Notice the : Geom(geometry) part. This is where the constructor of Geom is called and if you don't put something like that in your constructor then the default one is called.
Also, I'm almost certain that you have a bug in your constructor. It probably should be stdstrContainerName = strName; and not stdstrContainerName = stdstrContainerName;. The same applies to stdstrPluginType.
One more thing, this is not a bug and is technically correct but passing objects of types like std::string or Geometry (that is objects which might be 'heavy') might be decrease performance so why don't you pass them by reference? But that's not a bug (at least not in the piece of code you posted) and isn't directly related to your question.
The Container's constructor:
Container::Container(std::string strName, std::string strType, Geometry geometry)
stdstrContainerName = stdstrContainerName;
stdstrPluginType = stdstrPluginType;
Geom = geometry;
doesn't have any explicit initialization of the Geom field. It is first default initialized hence the default constructor call and then you assign it the geometry argument.
To achive what you want, you need to define Container's constructor this way:
Container::Container(std::string strName, std::string strType, Geometry geometry)
: Geom(geometry)
stdstrContainerName = strName;
stdstrPluginType = strType;
Notice the : Geom(geometry) part. This is where the constructor of Geom is called and if you don't put something like that in your constructor then the default one is called.
Also, I'm almost certain that you have a bug in your constructor. It probably should be stdstrContainerName = strName; and not stdstrContainerName = stdstrContainerName;. The same applies to stdstrPluginType.
One more thing, this is not a bug and is technically correct but passing objects of types like std::string or Geometry (that is objects which might be 'heavy') might be decrease performance so why don't you pass them by reference? But that's not a bug (at least not in the piece of code you posted) and isn't directly related to your question.
answered May 24 at 6:11
navybluenavyblue
56627
56627
add a comment |
add a comment |
Constructor #1 is called for the Geometry geometry argument passed by value to the constructor of Container. Because you are passing it by value, it is recreated on the stack of the constructor of Container. Change it to const Geometry& geometry.
How can i change it to be passed for the appropriate constructor ?
– shomit
May 24 at 5:55
Passing by reference is a good suggestion but it doesn't solve the problem in the question and has actually nothing to do with calling the default constructor. For details see my answer to the question or the answer of TrebledJ.
– navyblue
May 24 at 6:13
add a comment |
Constructor #1 is called for the Geometry geometry argument passed by value to the constructor of Container. Because you are passing it by value, it is recreated on the stack of the constructor of Container. Change it to const Geometry& geometry.
How can i change it to be passed for the appropriate constructor ?
– shomit
May 24 at 5:55
Passing by reference is a good suggestion but it doesn't solve the problem in the question and has actually nothing to do with calling the default constructor. For details see my answer to the question or the answer of TrebledJ.
– navyblue
May 24 at 6:13
add a comment |
Constructor #1 is called for the Geometry geometry argument passed by value to the constructor of Container. Because you are passing it by value, it is recreated on the stack of the constructor of Container. Change it to const Geometry& geometry.
Constructor #1 is called for the Geometry geometry argument passed by value to the constructor of Container. Because you are passing it by value, it is recreated on the stack of the constructor of Container. Change it to const Geometry& geometry.
edited May 24 at 5:55
answered May 24 at 5:54
goodvibrationgoodvibration
1,608426
1,608426
How can i change it to be passed for the appropriate constructor ?
– shomit
May 24 at 5:55
Passing by reference is a good suggestion but it doesn't solve the problem in the question and has actually nothing to do with calling the default constructor. For details see my answer to the question or the answer of TrebledJ.
– navyblue
May 24 at 6:13
add a comment |
How can i change it to be passed for the appropriate constructor ?
– shomit
May 24 at 5:55
Passing by reference is a good suggestion but it doesn't solve the problem in the question and has actually nothing to do with calling the default constructor. For details see my answer to the question or the answer of TrebledJ.
– navyblue
May 24 at 6:13
How can i change it to be passed for the appropriate constructor ?
– shomit
May 24 at 5:55
How can i change it to be passed for the appropriate constructor ?
– shomit
May 24 at 5:55
Passing by reference is a good suggestion but it doesn't solve the problem in the question and has actually nothing to do with calling the default constructor. For details see my answer to the question or the answer of TrebledJ.
– navyblue
May 24 at 6:13
Passing by reference is a good suggestion but it doesn't solve the problem in the question and has actually nothing to do with calling the default constructor. For details see my answer to the question or the answer of TrebledJ.
– navyblue
May 24 at 6:13
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f56286563%2fhow-to-call-the-appropriate-constructor%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown