You are not logged in.

sumsin

Trainee

  • "sumsin" started this thread

Posts: 75

Location: INDIA

  • Send private message

1

Thursday, August 18th 2005, 8:26am

constructor

Hi

Actually I am confused abuot "What does a conctructor return?", or "What does a constructor really do?".

In the code given below:


Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Test {
 	public:
   		static Test create(int x);
   
 	private:
   		Test(int x);
                 int x_;
};
 
inline Test::Test(int x)
   : x_(x){
}
 
inline Test Test::create(int x) { 
	return Test(x);    // calling  constructor
}

int main(int argc, char **argv)
{
	Test t = Test::create(5);  // calling static method
	return 0;
}


In the code the return type of static method create is Test, and in the method create, it is returning Test(x), which is constructor of the class. So should it mean the return type of constructor is Test?.

bye

  • "wysota" is male

Posts: 4,276

Location: Warsaw, POLAND

  • Send private message

2

Thursday, August 18th 2005, 10:03am

The constructor doesn't have a return type. Treat it as a procedure (hence return type would be 'void') which initializes the object it is called on.

If you call the constructor directly, an object is created and initialized with the given constructor and returned to the caller.

3

Thursday, August 18th 2005, 10:30am

RE: constructor

One could say that calling the constructor 'returns' an object (instance) of this class, but it is not a return value as such.
In

Source code

1
Test * t = new Test(1); 
it is not the constructor returning the object but the new operator.
There's no place like ::1

sumsin

Trainee

  • "sumsin" started this thread

Posts: 75

Location: INDIA

  • Send private message

4

Thursday, August 18th 2005, 2:39pm

Hi

wysota:

You said that we can treat a constructor as a procedure of return type void, but as we did not mention any return type with constructor so by default its return type should be int.

and also would you please describe a little bit more your second paragraph.

zztop:

but waht is the role of Test(1) in the code.

bye.

5

Thursday, August 18th 2005, 3:06pm

> You said that we can treat a constructor as a procedure of return type void, but as we did > not mention any return type with constructor so by default its return type should be int.

There is no "return int if unspecified" rule in Standard C++. Every function must be declared completely, including its return type, except for class constructors and destructors.
In reality, you cannot call the constructor directly in almost all cases, excluding some special ones like constructor intializer lists etc.
A class is a user-defined type. A constructor is a special piece of code that runs whenever you instatiate an object of this class. Its sole purpose is to set the object's internal data structures, and you can never return anything from the c-tor.

When you say return Test(x), you don't actually call the constructor directly, since Type(value) is a C++ statement which returns a Type initialized with an appropriate value (it does call the constructor behind the scenes, but you can evaluate Test(x) outside the class, and it would still be Test(x), not Test::Test(x), so this is not a direct call of the constructor).

6

Thursday, August 18th 2005, 3:16pm

The new operator (m)allocs memory for fitting in an object of type class and then calls the member Test(int) to fill it with default values (initializing the object), thus what should it have as a return value? void? (that's what wysota already said) New returns a pointer to the newly allocated memory block.

Test(1) calls the member method Test (to initialize an object of type test) with an argument of type int.

Calling the constructor directly means creating a variable of type Test

Source code

1
2
3
Test t
// or
Test t();
on the stack

In this code a local variable called t is created and initialized by calling the constructor of Test. Afterwards it can be used like any other local variable

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <stdio.h>

class Test {
 	public:
   		static Test create(int x);
                void dump(void);
 	private:
   		Test(int x);
                 int x_;
};
 
inline Test::Test(int x)
   : x_(x){
   printf ("Constructor called with argument %i\n", x);
}
 
inline Test Test::create(int x) { 
	return Test(x);    // calling  constructor
}

inline void Test::dump(void)
{
 printf ("Value: %i\n", x_);
}

int main(int argc, char **argv)
{
	Test t = Test::create(5);  // calling static method
        t.dump();
	return 0;
}

Executing this program should result in:

Source code

1
2
Constructor called with argument 5
Value: 5
There's no place like ::1

sumsin

Trainee

  • "sumsin" started this thread

Posts: 75

Location: INDIA

  • Send private message

7

Thursday, August 18th 2005, 4:44pm

hi

kalkin:

Quoted

In reality, you cannot call the constructor directly in almost all cases, excluding some special ones like constructor intializer lists etc.


but how can we call a constructor in constructor intializer lists. Can you give some example( sample code ).


bye

  • "wysota" is male

Posts: 4,276

Location: Warsaw, POLAND

  • Send private message

8

Thursday, August 18th 2005, 5:06pm

Quoted

Originally posted by sumsin
but how can we call a constructor in constructor intializer lists. Can you give some example( sample code ).



You have it in the code you provided:

Source code

1
2
3
inline Test::Test(int x)
   : x_(x){
}


member variable x_ from class Test is initialised with the value x.

9

Thursday, August 18th 2005, 5:10pm

Well, we don't actually call it (as if it was a call of a function within our flow of control).
I'd probably be explaining better by example :)

suppose we have a basic class:

Source code

1
2
3
4
5
6
7
class Basic {
       int a;
public:
       Basic(int i) { a = i }  // this can also be written using an initializer list as
                                       // Basic(int i) : a(i) {}
       inline int v() const { return a; }
};

and its fuzzy descendant

Source code

1
2
3
4
5
6
7
class Fuzzy : public Basic {
        double f;
public:
         Fuzzy(int a, double d) : Basic(a) { f = d; } // Here is the the call to the constructor of
                                                                           // our base class, Basic(a)
         inline double d() const { return f; }
};

So, when you instantiate a Fuzzy object with, for example,

Fuzzy fuzz(2, 3.14);

the Basic "slice" of fuzz gets initialized with a == 2, so that fuzz.v() will return 2.