By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,918 Members | 1,247 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,918 IT Pros & Developers. It's quick & easy.

Calling Explicit Constructer

P: n/a
> Hi All,
Some Strange thing with C++ Constructer !! I never knew that we can initialize an object / a variable more than once using C++ Constructer till I started doing some RnD coding!!!! And its going against concept of initialization (which means give some value
to a variable when its been allocated in memory which we can do only once, atleast theorotically). And in C++, Constructer serves the same (means initializes the Class Object) Now look at the code below. I am initializing one (of type OneClass) which is Member of Class TwoClass (once implicit constructer which is been called
when doing one(val) (in intialization list) and secong time by calling explicit constructer using one.OneClass::OneClass(val+10); Now does the second one is initialization or assignment (If so, Damm...!!! Constructer is called here for assignment, which is meant to be for Initialization !!!!) class OneClass { int a; public: OneClass (int val = 0) { if(a>0) a+=val; else a=val; } void Set (int val) { a = val; } int Get () { return a; } }; class TwoClass { OneClass one; public: TwoClass (int val = 0) : one(val) { one.OneClass::OneClass(val+10); } void Set (int val) { one.Set (val); } int Get () { return one.Get (); } }; int main () { TwoClass two(5); cout<<"Value is : "<<two.Get()<<endl; } And the output is Value is : 20 Any comments on this? Regards Girish

Jul 23 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On which compiler you had compiled this code. It does not work. The
line

one.OneClass::OneClass(val+10)*;

gives an error. Meanwhile you can never call a constructor as if it a
member function.

Jul 23 '05 #2

P: n/a
Your formatting really sucks, with excessive spacing and '>' characters that
stop anyone from doing a straight paste into their IDE and having the code
compile.

The bottom line is that your code invokes something compiler-specific (VC++
at a guess) and is not standard C++. Comeau online

http://www.comeaucomputing.com/tryitout/

won't compile your code at all.

--
John Carson
Jul 23 '05 #3

P: n/a
Girish Shetty wrote:
Hi All,

Some Strange thing with C++ Constructer !!

I never knew that we can initialize an object / a variable more than once

using C++ Constructer till I started doing some RnD coding!!!!


You cannot explicitely call a constructor. A constructor is always called
for you when you create an object.
TwoClass (int val = 0) : one(val) { one.OneClass::OneClass(val+10); }

Must be a compiler bug.
Constructors don't have names and thus cannot participate in function
name lookup. As a consequence you cannot call them directly using
ordinary call syntax. If your compiler allows this, it must be a bug in
your compiler.
Any comments on this?


Please do a better formatting of your postings.
--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #4

P: n/a
Girish Shetty wrote:
Hi All,
Some Strange thing with C++ Constructer !!
I never knew that we can initialize an object / a variable more than once


No, that doesn't make sense.
using C++ Constructer till I started doing some RnD coding!!!!
And its going against concept of initialization (which means give some
value
to a variable when its been allocated in memory which we can do only once,
atleast theorotically). And in C++, Constructer serves the same (means
initializes the Class Object)
You can use a constructor to do initialisation of assignment.
Now look at the code below. I am initializing one (of type OneClass) which
is Member of Class TwoClass (once implicit constructer which is been
called

when doing one(val) (in intialization list) and secong time by calling

explicit constructer using one.OneClass::OneClass(val+10);

Now does the second one is initialization or assignment (If so, Damm...!!!

Constructer is called here for assignment, which is meant to be for

Initialization !!!!)

class OneClass {
int a;
public:
OneClass (int val = 0) {
if(a>0) a+=val;
else a=val;
}
That constructor does assignment to a, not initialisation.
void Set (int val) { a = val; }

int Get () { return a; }
};

class TwoClass {
OneClass one;
public:

TwoClass (int val = 0) : one(val) { one.OneClass::OneClass(val+10); }


I don't know what that is.
Any comments on this?


class ClassA {
private:
int a;

public:
ClassA(int val) : a(val) {}
};

class ClassB {
private:
int b;

public:
ClassB(int val) {
b = val;
}
};

The member of ClassA is initialised.

The member of ClassB is assigned (you can tell be the = sign)

Get it?
#include <iostream>

class OneClass {
private:
int a;

public:
OneClass(int val) : a(val) {}
int getA(void) {return a;}
};

class TwoClass {
private:
OneClass one;

public:
TwoClass(int val) : one(val) {}
OneClass& getOne() {return one;}
};

int main(int argc, char* argv[])
{
TwoClass two(2);

std::cout << two.getOne().getA() << std::endl;

return 0;
}

Instantiating TwoClass in main, calls the (TwoClass) constructor initialiser list, which calls the constructor for OneClass, which calls the (OneClass) initialiser list, so both
TwoClass and the OneClass member of TwoClass are initialised, without any assignemnt.

Is that what you wanted?

Ben
--
I'm not just a number. To many, I'm known as a String...
Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.