Not using parentheses in constructor call with new (c++) [duplicate] - c++

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Do the parentheses after the type name make a difference with new?
So I had in my main:
Class* pC = new Class;
It was working as
Class* pC = new Class();
I realized just today that I had omitted the parentheses (so I was hit by the "opposite" of the most vexing parse in a way).
My question: Are these two forms equivalent ?

If the class has a default constructor defined, then both are equivalent; the object will be created by calling that constructor.
If the class only has an implicit default constructor, then there is a difference. The first will leave any members of POD type uninitialised; the second will value-initialise them (i.e. set them to zero).

Related

What happens when I try to create an object using its empty default constructor but while adding brackets in ANSI C++? [duplicate]

This question already has answers here:
Default constructor with empty brackets
(9 answers)
Closed 4 years ago.
I have a class called "Complex" which is intended to represent complex numbers. I have defined an empty default constructor for the class that only prints a message to the screen.
When I try to create an object of the class in the main function as follows:
Complex c1();
The compiler(I am using BorlandC) doesn't give a syntax error but it doesn't create the object. How does the compiler interpret this line?
When you write this:
int foo();
…it declares a function called foo that returns an int.
When you write this:
Complex c1();
…it declares a function called c1 that returns a Complex.
Lose the ().
Contrary to popular belief, this is not quite "the most vexing parse", but it is close.

C++ - Incomplete class type? [duplicate]

This question already has answers here:
"Incomplete type" in class which has a member of the same type of the class itself
(8 answers)
Closed 6 years ago.
I've been working with a lot of C and Java lately so I'm a bit confused coming back to C++ on why this is not allowed.
incomplete type is not allowed
#pragma once
class Expression
{
private:
Expression power; // <--- incomplete type is not allowed
};
I believe the answer here is to change the line Expression power to Expression *power but I don't understand why that is. I can declare objects like vector<int> var without having to make them a pointer, but the second I have an object of the same type as the file it's being declared in, I need one? I've looked around but cannot find any tutorials/videos on a class making an object of itself.
If you put an instance of a class inside itself, if you notice, you are creating recursion, since every instance has its own Expression power, and this goes on forever. If you have a pointer though, you can control this infinite recursion, since at any time you can set power to nullptr, (or NULL/0, pre C++11), to end the recursion.

Why do constructors need to be named exactly after the class? [duplicate]

This question already has answers here:
Why constructors will always have same name as of class and how they are invoked implicitly?
(7 answers)
Closed 8 years ago.
What is the reason a constructor needs to have the exact same name than the class? Is it purely a sintactic reason? Is it possible to define the constructor with a different name?
It needs to have the same name as the class to tell it apart from other (non-special) member functions. This is a convention for the constructor (and destructor, when prefixed with a tilde) to unambiguously tell the compiler that you are creating a constructor for your class and not just a member function in the class.

If I put an argument in my default constructor, but give the argument a default value, is it still really a default constructor? [duplicate]

This question already has an answer here:
In C++, is a constructor with only default arguments a default constructor?
(1 answer)
Closed 9 years ago.
I know it's said to be a default constructor, but how does it work behind the scenes? I'm getting a "procedure entry point could not be located" error when my program tries to use my library containing class A with this default constructor. The program doesn't even use the default constructor of class A; it uses other constructors of the A. The library builds fine; the program builds fine. The DLL has been rebuilt, so it should know its own method call when it sees it. I'm actually just completely lost.
class DLLEXPORT A
{
A(int a = 0); //default constructor and single parameter constructor
};
A default constructor is any constructor that is callable with no arguments.
When you say A x;, then this is the same as A x(0);, which is what the compiler actually calls.
Similarly, copy constructors can have additional, defaulted arguments.

What Does THIS Do? C++ [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What is this weird colon-member syntax in the constructor?
C++ initialization
I have just received a header file in a C++ program, and I cannot figure out what this line of code does:
Card(Value faceValue=deuce, Suit suit = clubs):
suit(suit), faceValue(faceValue) {}
What does the : mean, and why does replacing it with a ; (as I thought I should) break the code?
Sorry for the generalness of this question, but could someone please explain the purpose of these two lines?
Thank you for your time.
This looks like a constructor for the Card class. The part after the : is an initializer list, initializing the values of member variables (or parent classes, but I don't think that's applicable in this case). The body of the constructor is empty because everything it needed to do was done in the initializer list.
The : and what follow is the initialization list. The reason you use it instead of assigning the member variables in the constructor's body is that if you do it inside the constructors body, the default constructor will be called first and then the copy constructor or assignment operator will be called afterwards. By using the initialization list you skip the first step.
Initialization lists. It's the preferred way to initialize class constructors in C++.
It is used because it allows the initialization of const members of the class without compilation error.