Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constructor not returning usable object

I have a problem with the constructor, which is not working as I'd expect.

If I try to initialize my class like that, it will work and I get a usable object:

vector<float> v;
MyClass<2> a(v);

However, if I try to build a class like below (which should be equivalent) the results are quite unexpected. There is no error message/warning when compiling or running the program. But if you try to use this variable a somewhere and call its methods (for example a.doSomething()), it will crash.

I put some code inside the constructor to notify me if it is used. It turned out that no code inside the constructor was actually executed in this case.

MyClass<2> a(vector<float>());

So I am wondering why this is happening? Is the 2nd declaration illegal?

EDIT: I will post some code of the class

template <int x>
class MyClass {
public:
    vector<float> v;
    MyClass<x>(vector<float> v1) {
      v = v1;
    }

};
like image 672
dominos Avatar asked Mar 19 '11 17:03

dominos


People also ask

Why does the constructor not return any value?

So the reason the constructor doesn't return a value is because it's not called directly by your code, it's called by the memory allocation and object initialization code in the runtime. Its return value (if it actually has one when compiled down to machine code) is opaque to the user - therefore, you can't specify it.

Does constructor return any value in C++?

You do not specify a return type for a constructor. A return statement in the body of a constructor cannot have a return value.

Why there is no return type for constructor in C++?

The constructor in C++ has the same name as the class or structure. Constructor is invoked at the time of object creation. It constructs the values i.e. provides data for the object which is why it is known as constructors. Constructor does not have a return value, hence they do not have a return type.

Which constructor does not have return type?

No, constructor does not have any return type in Java. Constructor looks like method but it is not. It does not have a return type and its name is same as the class name. Mostly it is used to instantiate the instance variables of a class.


1 Answers

MyClass<2> a(vector<float>());

This is not a variable declaration. It is the declaration of a function named a that returns a MyClass<2> object and takes as an argument a "pointer to a function that takes no arguments and returns a vector<float>." Confusing? Yes. This is what is referred to as the "most vexing parse."

You need extra parentheses:

MyClass<2> a((vector<float>()));
             ^               ^

Or, you can use copy initialization:

MyClass<2> a = MyClass<2>(vector<float>());

Or, since your constructor isn't explicit, you could use:

MyClass<2> a = vector<float>();

(Though, unless you mean for vector<float> objects to be implicitly convertible to MyClass<N> objects, you probably want to make this constructor explicit.)


A good compiler should warn you about this sort of thing. Visual C++ warns:

warning C4930: 'MyClass<x> a(std::vector<_Ty> (__cdecl *)(void))': prototyped function not called (was a variable definition intended?)

Clang warns:

warning: parentheses were disambiguated as a function declarator

MyClass<2> a(vector<float>());
            ^~~~~~~~~~~~~~~~~
like image 181
James McNellis Avatar answered Oct 14 '22 05:10

James McNellis