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
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