shortcut constructor syntax

C

cppaddict

First question: What is the term for this method of creating an A
object:

A a("constructor string parameter");

Second question: How come you can't use this method with a constructor
that takes no parameters?

Just in case you can, and the reason that I think you can't is an
error in my test code, here is the test code:

class A {
private:
int _x;
public:
int getX() {return _x;}
A();
A(int x);
};
A::A() : _x(10) {
std::cout << "Making A\n";
}
A::A(int x) : _x(x) {
std::cout << "Making A\n";
}


****This version of main() works******
int main() {
A a(9);
std::cout << a.getX() << std::endl;
return 0;
}

****This version of main() won't compile******
int main() {
A a();
std::cout << a.getX() << std::endl;
return 0;
}

The compiler errors on the "A a();" line with:
Error E2294 Main.cpp 34: Structure required on left side of . or .*
in function main()

Thanks for any explanations,
cpp
 
S

Sharad Kala

cppaddict said:
First question: What is the term for this method of creating an A
object:

A a("constructor string parameter");

This means that A class ctor takes either std::string/char* as parameter.

Second question: How come you can't use this method with a constructor
that takes no parameters?
[snip]
****This version of main() won't compile******
int main() {

The above is a declaration of function named a which returns A.

-Sharad
 
R

Rolf Magnus

cppaddict said:
First question: What is the term for this method of creating an A
object:

A a("constructor string parameter");

That's a variable definition.
Second question: How come you can't use this method with a constructor
that takes no parameters?

Because the meaning is different. If you write:

A a();

you declare (not define) a function (not a variable) that takes no
parameters and returns an A. But you can just do:

A a;

or if you insist on the parens:

A a = A();
Just in case you can, and the reason that I think you can't is an
error in my test code, here is the test code:

class A {
private:
int _x;
public:
int getX() {return _x;}
A();
A(int x);
};
A::A() : _x(10) {
std::cout << "Making A\n";
}
A::A(int x) : _x(x) {
std::cout << "Making A\n";
}


****This version of main() works******
int main() {
A a(9);
std::cout << a.getX() << std::endl;
return 0;
}

****This version of main() won't compile******
int main() {
A a();
std::cout << a.getX() << std::endl;
return 0;
}

The compiler errors on the "A a();" line with:

Sure that it wasn't the next line?
 
J

jeffc

cppaddict said:
First question: What is the term for this method of creating an A
object:

A a("constructor string parameter");

What is the context of your question? The fact that the constructor has a
parameter? I'd just say instantiating an A object with the constructor that
takes a string parameter.
Second question: How come you can't use this method with a constructor
that takes no parameters?

Because there's no way for the compiler to tell the difference between that
and a function declaration. For example, if I asked you to declare a
function called "a" that has no parameters and returns an A, how would you
do it?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,172
Messages
2,570,934
Members
47,474
Latest member
AntoniaDea

Latest Threads

Top