473,320 Members | 1,910 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

fail to call another constructor from constuctor

hi

1 #include <stdio.h>
2
3 class A{
4 private:
5 int x;
6 public:
7 A(int x){
8 this->x;
9 }
10
11 A(int x, char *y){
12 A(x); <---------------- may
i know how to fix this line?
13 }
14 };
15
16 int main(){
17 return 1;
18 }

d.cpp: In constructor `A::A(int, char*)':
d.cpp:12: error: declaration of `x' shadows a parameter
d.cpp:12: error: no matching function for call to `A::A()'
d.cpp:3: error: candidates are: A::A(const A&)
d.cpp:11: error: A::A(int, char*)
d.cpp:7: error: A::A(int)

thanks
from Peter (cm****@hotmail.com)

Oct 5 '06 #1
18 2829
just explicitly call constructor A() as follows:
A::A(x); //line 12
cm****@hotmail.com wrote:
hi

1 #include <stdio.h>
2
3 class A{
4 private:
5 int x;
6 public:
7 A(int x){
8 this->x;
9 }
10
11 A(int x, char *y){
12 A(x); <---------------- may
i know how to fix this line?
13 }
14 };
15
16 int main(){
17 return 1;
18 }

d.cpp: In constructor `A::A(int, char*)':
d.cpp:12: error: declaration of `x' shadows a parameter
d.cpp:12: error: no matching function for call to `A::A()'
d.cpp:3: error: candidates are: A::A(const A&)
d.cpp:11: error: A::A(int, char*)
d.cpp:7: error: A::A(int)

thanks
from Peter (cm****@hotmail.com)
Oct 5 '06 #2
laikon <la****@gmail.comwrote:
>just explicitly call constructor A() as follows:
A::A(x); //line 12
That never works when I try it. For example, the following
compiles with gcc but fails to print out a value of "3" --
it prints a garbage value.

I'm sure there's a simple explanation, but pending figuring out
what it is, I avoid calling one constructor from another.

Steve

*******
#include <iostream>

struct A {
int state;
A(int x) {
state = x;
};
A() {
A::A(3);
};
};

int main()
{
using std::cout;
using std::endl;
A z;
cout << z.state << endl;
}
Oct 5 '06 #3
cm****@hotmail.com wrote:
hi

1 #include <stdio.h>
2
3 class A{
4 private:
5 int x;
6 public:
7 A(int x){
8 this->x;
9 }
10
11 A(int x, char *y){
12 A(x); <---------------- may
i know how to fix this line?
13 }
14 };
15
16 int main(){
17 return 1;
18 }

d.cpp: In constructor `A::A(int, char*)':
d.cpp:12: error: declaration of `x' shadows a parameter
d.cpp:12: error: no matching function for call to `A::A()'
d.cpp:3: error: candidates are: A::A(const A&)
d.cpp:11: error: A::A(int, char*)
d.cpp:7: error: A::A(int)

thanks
from Peter (cm****@hotmail.com)

See:
http://www.parashift.com/c++-faq-lit....html#faq-10.3

Regards,
Sumit.
Oct 5 '06 #4
On 4 Oct 2006 20:47:54 -0700, cm****@hotmail.com wrote:
>hi

1 #include <stdio.h>
2
3 class A{
4 private:
5 int x;
6 public:
7 A(int x){
8 this->x;
9 }
10
11 A(int x, char *y){
12 A(x); <---------------- may
i know how to fix this line?
13 }
14 };
15
16 int main(){
17 return 1;
18 }
The answer is: This is not Java, and such a way to cascade
constructors is not allowed (but it may change in the future...)
Regards,

Zara
Oct 5 '06 #5

cm****@hotmail.com wrote:
1 #include <stdio.h>
2
3 class A{
4 private:
5 int x;
6 public:
7 A(int x){
8 this->x;
9 }
10
11 A(int x, char *y){
12 A(x); <---------------- may
i know how to fix this line?
13 }
14 };
15
16 int main(){
17 return 1;
18 }

d.cpp: In constructor `A::A(int, char*)':
d.cpp:12: error: declaration of `x' shadows a parameter
d.cpp:12: error: no matching function for call to `A::A()'
d.cpp:3: error: candidates are: A::A(const A&)
d.cpp:11: error: A::A(int, char*)
d.cpp:7: error: A::A(int)
This is an FAQ:

http://www.parashift.com/c++-faq-lit....html#faq-10.3

Best regards,

Tom

Oct 5 '06 #6
Steve Pope wrote:
laikon <la****@gmail.comwrote:
>>just explicitly call constructor A() as follows:
A::A(x); //line 12

That never works when I try it. For example, the following
compiles with gcc but fails to print out a value of "3" --
it prints a garbage value.

I'm sure there's a simple explanation, but pending figuring out
what it is, I avoid calling one constructor from another.

Steve

*******
#include <iostream>

struct A {
int state;
A(int x) {
state = x;
};
A() {
A::A(3);
This creates a temporary of type A and initializes it. Then the temporary is
destructed. Pending side effects, this is a null op.
};
};

int main()
{
using std::cout;
using std::endl;
A z;
cout << z.state << endl;
}
You would need to call the constructor for the given object:
#include <iostream>

struct A {
int state;
A(int x)
: state(x)
{}

A() {
new (this) A(3);
}
};

int main()
{
using std::cout;
using std::endl;
A z;
cout << z.state << endl;
}
However, I it might be undefined behavior (I am not sure: 3.8/5 comes to
mind but I am a little too lazy right now to embark on a careful exegesis
right now).
Best

Kai-Uwe Bux
Oct 5 '06 #7

Steve Pope wrote:
laikon <la****@gmail.comwrote:
just explicitly call constructor A() as follows:
A::A(x); //line 12

That never works when I try it. For example, the following
compiles with gcc but fails to print out a value of "3" --
it prints a garbage value.
Constructors are not called, they are invoked. If you want the default
state to be initialized to 3, then initialize state via the
initialization list from within a default ctor. Calling A::A(3) simply
creates a temporary which dies immediately.
>
I'm sure there's a simple explanation, but pending figuring out
what it is, I avoid calling one constructor from another.
Thats what the init list does, instead of creating and then
initializing the state integer:
A::A()
{
state = 3;
}

You call state's ctor directly - instantaneous allocation +
initialization
A::A() : state(3)
{
}
>
Steve

*******
#include <iostream>

struct A {
int state;
A(int x) {
state = x;
};
A() {
A::A(3);
};
};

int main()
{
using std::cout;
using std::endl;
A z;
cout << z.state << endl;
}
try:

#include <iostream>

struct A
{
A() : state(3) { }
A(int n) : state(n) { }
int getState() const { return state; }
private:
int state;
};

int main()
{
A a; // invoke the default ctor
std::cout << "a = " << a.getState() << std::endl;
A b(101); // invoke the parametized ctor
std::cout << "b = " << b.getState() << std::endl;
}

/*
a = 3
b = 101
*/

Oct 5 '06 #8
Kai-Uwe Bux <jk********@gmx.netwrote:
>Steve Pope wrote:
>laikon <la****@gmail.comwrote:
>>>just explicitly call constructor A() as follows:
A::A(x); //line 12
>That never works when I try it. For example, the following
compiles with gcc but fails to print out a value of "3" --
it prints a garbage value.
>I'm sure there's a simple explanation, but pending figuring out
what it is, I avoid calling one constructor from another.
>*******

#include <iostream>

struct A {
int state;
A(int x) {
state = x;
};
A() {
A::A(3);
};
};

int main()
{
using std::cout;
using std::endl;
A z;
cout << z.state << endl;
}

This creates a temporary of type A and initializes it. Then the temporary is
destructed. Pending side effects, this is a null op.
Right; if I replace A::A(3) with float(3) I get a "warning statement
has no effect", but A::A(3) issues no warning ... as you point out
it may have a side effect.
>
You would need to call the constructor for the given object:

#include <iostream>

struct A {
int state;
A(int x)
: state(x)
{}

A() {
new (this) A(3);
}
};

int main()
{
using std::cout;
using std::endl;
A z;
cout << z.state << endl;
}
However, I it might be undefined behavior (I am not sure: 3.8/5 comes to
mind but I am a little too lazy right now to embark on a careful exegesis
right now).
Thanks Kai-Uwe. If in this situations I will separate out
the code common to the two constructors into a member function:
#include <iostream>

struct A {
int state;
void setState(int x) { state = x; };
A(int x) {
setState(x);
};
A() {
setState(3);
};
};

int main()
{
using std::cout;
using std::endl;
A z;
cout << z.state << endl;
}
Oct 5 '06 #9

cm****@hotmail.com wrote:
>
1 #include <stdio.h>
2
3 class A{
4 private:
5 int x;
6 public:
7 A(int x){
8 this->x;
9 }
10
11 A(int x, char *y){
12 A(x); <---------------- may
i know how to fix this line?
13 }
14 };
15
16 int main(){
17 return 1;
18 }
It's meaningless code. this->x doesn't do anything. You should say
this->x( x ) in the initialiser but better not to call the parameter x
at all.

You cannot call one constructor from another but there are two
workarounds:

class A
{
private:
void init ( int x );

public:
/*explicit*/ A( int x )
{
init( x );
}

A( int x, char * y )
{
init ( x );
// do other stuff
}
};

Then you can use private inheritance. That does allow you to call a
constructor from a constructor - the constructor of your base class.
You can have two of your constructors call the base class constructor.
Because the inheritance is private, it is implementation detail of your
class.

class XInit
{
friend class A;

XInit( int x );
};

class A : XInit
{

public:
/*explicit*/ A( int x ) : XInit( x )
{
// do stuff if you want
}

A( int x, char * y )
: XInit( x )
{
// do other stuff
}
};

Here XInit is really only created for the benefit of A and it doesn't
therefore really matter that the two are so tightly coupled. Note that
by making A a friend of XInit and everything in it private, nothing
else at all can use XInit, not even by deriving from it. You might
shove it in a namespace to avoid namespace pollution.

Oct 5 '06 #10
laikon wrote:
just explicitly call constructor A() as follows:
A::A(x); //line 12
That's not legal either.
You can't call constructors.

If you want to have code common to two constructors you need to
put it in another member function that they both can call.

Oct 5 '06 #11
Steve Pope wrote:
>
I'm sure there's a simple explanation, but pending figuring out
what it is, I avoid calling one constructor from another.
The simple explanation is that you can not call constructors.
The constructors are invoked for you by the implementation as
part of object creation. In specifiying arguments you can
affect which overload of the constructor is used, you can not
change the ordering or otherwise alter the process.
Oct 5 '06 #12
LR
Kai-Uwe Bux wrote:
Steve Pope wrote:

>>laikon <la****@gmail.comwrote:
>>#include <iostream>

struct A {
int state;
A(int x) {
state = x;
};
A() {
A::A(3);


This creates a temporary of type A and initializes it. Then the temporary is
destructed. Pending side effects, this is a null op.

> };
};
You would need to call the constructor for the given object:
#include <iostream>

struct A {
int state;
A(int x)
: state(x)
{}

A() {
new (this) A(3);
}
};
>
However, I it might be undefined behavior (I am not sure: 3.8/5 comes to
mind but I am a little too lazy right now to embark on a careful exegesis
right now).
There may be other worries for other classes.

For example, if your class inherits from a class that has a pointer
where the default ctor allocates memory then I think new (this) might
result in a memory leak..

Can we make a swap member function of A?

void A::swap(A &a) {
std::swap(a.x,x);
}

and rewrite the ctor that we want to call another ctor?
A::A()
:
x()
{
A temp(3);
swap(temp);
}
LR
Oct 5 '06 #13
Kai-Uwe Bux posted:
A() {
new (this) A(3);
}

I advocate the use of:

::new(p)

instead of:

new(p)

when using placement new. The code should be all right, just so long as no
member objects or base-class objects have constructors which acquire
resources or something of the like. The following is doomed to break
because it will call "str"'s constructor twice... possible resulting in
leaked resource, or perhaps even worse.

#include <string>
#include <new>

class MyClass {
private:

std::string str;

public:

MyClass(int) {}

MyClass(double x) { ::new(this) MyClass(x/3 +5); }
};

--

Frederick Gotham
Oct 5 '06 #14
Frederick Gotham wrote:
Kai-Uwe Bux posted:
> A() {
new (this) A(3);
}


I advocate the use of:

::new(p)

instead of:

new(p)

when using placement new. The code should be all right, just so long as no
member objects or base-class objects have constructors which acquire
resources or something of the like. The following is doomed to break
because it will call "str"'s constructor twice... possible resulting in
leaked resource, or perhaps even worse.

#include <string>
#include <new>

class MyClass {
private:

std::string str;

public:

MyClass(int) {}

MyClass(double x) { ::new(this) MyClass(x/3 +5); }

};
Correct: that would be bad. However, once you are in a state of sin, why not
try:

#include <string>
#include <new>

class MyClass {
private:

typedef std::string string;
string str;

public:

MyClass(int) {}

MyClass(double x) {
(&str)->~string();
::new(this) MyClass(x/3 +5);
}
};

Note: I am *not* advocating this.
Best

Kai-Uwe Bux
Oct 5 '06 #15

Steve Pope wrote in message ...
>laikon <la****@gmail.comwrote:
>>just explicitly call constructor A() as follows:
A::A(x); //line 12

That never works when I try it. For example, the following
compiles with gcc but fails to print out a value of "3" --
it prints a garbage value.

I'm sure there's a simple explanation, but pending figuring out
what it is, I avoid calling one constructor from another.

Steve
*******

#include <iostream>

struct A {
int state;
A(int x) {
state = x;
};
A() {
A::A(3);
};
};

int main(){
using std::cout;
using std::endl;
A z;
cout << z.state << endl;
}
Hmmm, maybe you just want a default value?

struct A {
int state;
A( int x = 3 ) : state( x ) {};
};

int main(){
using std::cout;
using std::endl;
A z;
cout << z.state << endl;
A y(23);
cout << y.state << endl;
}

// -- output --
3
23

--
Bob R
POVrookie
Oct 5 '06 #16
Kai-Uwe Bux posted:
MyClass(double x) {
(&str)->~string();
::new(this) MyClass(x/3 +5);
}

Now all we need is a compiler that optimises-away a construction followed
immediately by a destruction ; ).
--

Frederick Gotham
Oct 5 '06 #17
LR
Frederick Gotham wrote:
Kai-Uwe Bux posted:

> A() {
new (this) A(3);
}

I advocate the use of:

::new(p)
This faq specifically recommends that placement new not be used in this
situation.
http://www.parashift.com/c++-faq-lit....html#faq-10.3

LR
Oct 5 '06 #18
BobR <Re***********@worldnet.att.netreplies to my post,
>Hmmm, maybe you just want a default value?

struct A {
int state;
A( int x = 3 ) : state( x ) {};
};
Thanks, that certainly works.

Steve
Oct 5 '06 #19

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: penny336 | last post by:
dear all, just a little confused in c++ oop , in the below class, i set x,y as private, but it can be accessed directly in copy constructor e.g x=p.x is it problems in c++? //=== file Point.h...
6
by: Gunnar G | last post by:
If I don't define a default constructor for my class "Foo", I still get one from the compiler. But if I define a constructor that takes an argument, I don't get the default constructor, why? I...
4
by: coke | last post by:
I am confused and need alittle help with a program i am writting and cannot for the life of me remember how to get the copy constuctor using stack class to work... i know exactly what i have to do...
9
by: Thomas Scheiderich | last post by:
I am curious as to 2 things. One is the order that the constructors are handled and the other is why the debugger doesn't show the static objects constructor (unless you set a breakpoint. For...
3
by: Jack Addington | last post by:
I am having a problem getting my inherited object to compile. I am trying to add another variable to the constuctor and I cannot get past different variations of the following message: No...
1
by: xxxxyz | last post by:
Hi, I want to create a class with a constructor which can fail (for example if (..) fail;). When constructor fail I want the variable to point to null. Example: class PosInt{ int i; public...
11
by: PengYu.UT | last post by:
The following program calls the normal constructor and the copy constructor. By calling the copy constuctor is redundandant, all I want is only a vector of a trial object. Is there any way to...
20
by: Michael | last post by:
Hi, There are two ways to set the class member data: 1. user-defined constructor function 2. default constuctor function + memeber function Could you please let me know when to use which....
1
by: kasiyil | last post by:
Hello, I have a question about constructors and destructors in C++. Assume that I declared a class that has a constructor and virtual destructor like below: class X { X() { printf...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.