Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Strange constructor question 2

Status
Not open for further replies.

Themuppeteer

Programmer
Apr 4, 2001
449
BE
I know there are different solutions to do following (one of them is the use of default values in the default constructor),but I would like to know why this isn't wright:

class A
{

A(){ cout<<&quot;constructor&quot;;}
A(int i){ A();
cout<<&quot;the number:&quot;<<i<<endl;}
}

I call my constructor from in my overloaded constructor.
No compiler error is given.

Why why why ??
The Muppeteer.

themuppeteer@hotmail.com

Don't eat yellow snow...
 
What's the question? Everything seems to be ok, except of that constructors should be public members.
 
yes sorry,I forgot the 'public:', I also thought
that this was ok, but in my testprogram,
the first constructor is not called when I call the
second constructor with argument. The answer I got here at work was that I should use default values and make only one constructor instead of 2. Sure that works,but why doesn't this example work ?
If I call here:
A m;
m(5);

the output will be 'the number'; and not 'constructor' like
one should expect.
The Muppeteer.

themuppeteer@hotmail.com

Don't eat yellow snow...
 
On my machine it looks better: &quot;constructorthe number:5&quot;
I'm using gcc 2.96 on RedHat 7.0
 
When I write this small program it works to :),
but in my big program it didn't do squad.
Perhaps I did something else wrong.
Anyhow,thnx for the confirmation and the time.
Do you think you can use the constructor as any ordinary function else in your class just to 're-init' stuff ? The Muppeteer.

themuppeteer@hotmail.com

Don't eat yellow snow...
 
I think, no. The
m.A(5);
results to compiler error.

m(5); in your previous example is also wrong - constructors can be used only by object creation:
A m(5);

 
I meant A m(5); it was a strange kind of typo :)
But what I tried to ask was:

this is legal:

A(int i){ A();cout<<&quot;i:&quot;<<endl;}

but what about this?:

void legal(){ A();}


The Muppeteer.

themuppeteer@hotmail.com

Don't eat yellow snow...
 
looks good, my compiler has nothing against it.
 
Great :), someone here claimed a constructor was a
special kind of method and I should not call it from somewhere else,cause it wont work.
I claimed the opposite,but my program didn't work so I did not have a point.You made my point.
(I'm not saying it is clean code,but its legal)
Thnx again for the effort. The Muppeteer.

themuppeteer@hotmail.com

Don't eat yellow snow...
 
This code is legal only in point of view of my compiler. :) I can not say anything more. Why dont you use more elegant thing:

class A
{
void init(void){ cout<<&quot;constructor&quot;;}

A(){ init();}
A(int i){ init();
cout<<&quot;the number:&quot;<<i<<endl;}

void legal(){ init();}
}
 
Reveiw of constructors:
class A
{
A();
A(int);
protected:
int num;
};

legal(int y){return A(y);}

class B : public class A
{
B(int x){A(x);}
//or B(int x):A(x){}
};

should all be leagal... Not the best way to do hings, but you can.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top