-
July 1st, 2008, 07:02 AM
#1
constructor function
Hi,
I have a class like
class Test{
public:
Test* fun()
{
return new Test; // I also can write that return new Test()
}
};
what is the difference between both the return statements.
Is there any thing called constructor function in C++
Can someone explain please
Thanks
-
July 1st, 2008, 07:47 AM
#2
Re: constructor function
Originally Posted by Rajesh1978
Hi,
I have a class like
class Test{
public:
Test* fun()
{
return new Test; // I also can write that return new Test()
}
};
what is the difference between both the return statements.
Is there any thing called constructor function in C++
Can someone explain please
Thanks
They do the same thing it seems. Omitting the parenthesis just means it implicitly gets default constructed.
AFAIK, the only time you can call a constructor explicitly is via an initialiser list:
Code:
#include <iostream>
class Test
{
public:
Test()
{
std::cout << "Base constructor." << std::endl;
}
};
class Der_Test : public Test
{
public:
Der_Test() : Test()
{
std::cout << "Derived constructor." << std::endl;
}
};
int main()
{
Der_Test test;
return 0;
}
-
July 1st, 2008, 11:54 AM
#3
Re: constructor function
People tend towards the first because it can be used to declare variables
whereas the latter looks like a function prototype to the compiler
--> error: function "Test testing123()" declared but not defined.
-
July 1st, 2008, 12:57 PM
#4
Re: constructor function
Actually there is a difference, look here, specially 10.2 section.
-
July 1st, 2008, 11:53 PM
#5
Re: constructor function
I went through the link provided
But found nothing extra
there you can find
suppose you have a class called List then
void f()
{
List x; // Local object named x (of class List)
...
}
and
void g()
{
List x(); // Function named x (that returns a List)
...
}
which is different in my case
Please see my first post
I clearly mension that during creation of a new object.
By the way what is the tag to use to get a well formated code in this forum
is this <code>.......</code>
thanks
-
July 2nd, 2008, 03:35 AM
#6
Re: constructor function
Originally Posted by Rajesh1978
By the way what is the tag to use to get a well formated code in this forum
is this <code>.......</code>
thanks
Change "<" and ">" to "[" and "]" and you've got it.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
July 2nd, 2008, 05:45 AM
#7
Re: constructor function
@STLdude
Actually there is a difference, look here, specially 10.2 section.
How is that any different than what I said?
@Rajesh
You did not clearly mention anything about function prototypes in your first post. You only asked about the difference with and without () when using operator new. To which you have been given a complete answer -- they are the same.
Last edited by Duoas; July 2nd, 2008 at 05:49 AM.
-
July 2nd, 2008, 06:02 AM
#8
Re: constructor function
You only asked about the difference with and without () when using operator new. To which you have been given a complete answer -- they are the same.
From my reading of the C++ Standard, I'd say that they are technically different since with the parentheses it is value initialisation and without it is default initialisation, but they effectively do the same thing in this case.
-
July 2nd, 2008, 10:16 AM
#9
Re: constructor function
Originally Posted by Duoas
@STLdude
How is that any different than what I said?
@Rajesh
You did not clearly mention anything about function prototypes in your first post. You only asked about the difference with and without () when using operator new. To which you have been given a complete answer -- they are the same.
You said
People tend towards the first because it can be used to declare variables
which is not correct. If you don't get section 10.2, then I wont' be able to explain any better.
Just to reiterate again:
Code:
List x; // Local object named x (of class List)
List x(); // Function named x (that returns a List)
-
July 2nd, 2008, 10:54 AM
#10
Re: constructor function
Originally Posted by STLDude
which is not correct. If you don't get section 10.2, then I wont' be able to explain any better.
STLDude, you and Duoas are saying the same thing where it concerns syntax. Duoas' statement is misleading because of the "people tend towards" (when actually it is a matter of choosing the appropriate syntax for the desired construct), but I am very sure that Duoas understands the difference as mentioned in section 10.2 of the FAQ that you linked to.
However, section 10.2 of that FAQ does not answer the question that Rajesh1978 is asking. Rajesh1978 is asking about the difference between a new expression with the new initialiser omitted and one for which the new initialiser is an empty pair of parentheses. The answer, according to the 2003 edition of the C++ standard, is that the former results in default initialisation if the type is a non-POD class type, or the creation of an object with indeterminate value otherwise. The latter results in value initialisation.
Now, from Rajesh1978's example, Test is POD class, therefore default initialisation results in zero initialisation. Since there is no user declared constructor, value initialisation results in value initialisation of non-static data members and base class components. However, as there are no member variables, there is nothing to initialise anyway, so the effect must be the same either way.
Last edited by laserlight; July 2nd, 2008 at 11:03 AM.
Reason: Oops, Test is a POD class.
-
July 2nd, 2008, 11:19 PM
#11
Re: constructor function
However, section 10.2 of that FAQ does not answer the question that Rajesh1978 is asking. Rajesh1978 is asking about the difference between a new expression with the new initialiser omitted and one for which the new initialiser is an empty pair of parentheses. The answer, according to the 2003 edition of the C++ standard, is that the former results in default initialisation if the type is a non-POD class type, or the creation of an object with indeterminate value otherwise. The latter results in value initialisation.
laserlight
You caught the exact point. This is the thing I was actually looking for. But it was driven in some other way.
Now I got the solution
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|