stack variable or declaration of function [duplicate] - c++

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Most vexing parse: why doesn't A a(()); work?
I have two classes in file1.h:
class ZoneRecord {
public:
//a lof of stuff here
};
class RegisterRecord {
public:
RegisterRecord(ZoneRecord rec); //this function register object rec in a fabric
};
And file2.cpp has:
#include "file1.h"
class MockZoneRecord: public ZoneRecord {
public:
MockZoneRecord(): ZoneRecord() {}
};
RegisterRecord mockrecord_register(MockZoneRecord());
This code compiles perfectly, except one thing. It says that mockrecord_register is a declaration of a function. But I actually wanted to create an global object of type RegisterRecord with name mockrecord_register. How to explicitly tell to compiler that this is not a function prototype, but an object?

You are experiencing the most vexing parse.
One way to solve this is to use copying, like
RegisterRecord mockrecord_register = RegisterRecord(MockZoneRecord());
Another is the use of parenthesis like in the answer by yuri kilochek.
If your compiler is C++11 compatible, you could use this construct:
RegisterRecord mockrecord_register{MockZoneRecord()};

Place parenthesis around argument:
RegisterRecord mockrecord_register((MockZoneRecord()));

Related

No output when class is created [duplicate]

This question already has answers here:
Default constructor with empty brackets
(9 answers)
Closed 4 years ago.
This code doesn't behave how I expect it to.
#include<iostream>
using namespace std;
class Class
{
Class()
{
cout<<"default constructor called";
}
~Class()
{
cout<<"destrutor called";
}
};
int main()
{
Class object();
}
I expected the output 'default constructor called', but I did not see anything as the output. What is the problem?
Nope. Your line Class object(); Declared a function. What you want to write is Class object;
Try it out.
You may also be interested in the most vexing parse (as others have noted). A great example is in Effective STL Item 6 on page 33. (In 12th printing, September 2009.) Specifically the example at the top of page 35 is what you did, and it explains why the parser handles it as a function declaration.
No call to constructor
Because the constructor never gets called actually.
Class object(); is interpreted as the declaration of a function object taking no argument and returning an object of Class [by value]
Try Class object;
EDIT:
As Mike noticed this is not exactly the same code as what you are feeding to the compiler. Is the constructor/destructor public or is Class a struct?
However google for C++ most vexing parse.
You can use it like this:
Class obj;
//or
Class *obj = new Class(/*constructor arguments*/);

What's the difference between MyClass myObject; and MyClass myObject();? [duplicate]

This question already has answers here:
Default constructor with empty brackets
(9 answers)
Closed 4 years ago.
This code doesn't behave how I expect it to.
#include<iostream>
using namespace std;
class Class
{
Class()
{
cout<<"default constructor called";
}
~Class()
{
cout<<"destrutor called";
}
};
int main()
{
Class object();
}
I expected the output 'default constructor called', but I did not see anything as the output. What is the problem?
Nope. Your line Class object(); Declared a function. What you want to write is Class object;
Try it out.
You may also be interested in the most vexing parse (as others have noted). A great example is in Effective STL Item 6 on page 33. (In 12th printing, September 2009.) Specifically the example at the top of page 35 is what you did, and it explains why the parser handles it as a function declaration.
No call to constructor
Because the constructor never gets called actually.
Class object(); is interpreted as the declaration of a function object taking no argument and returning an object of Class [by value]
Try Class object;
EDIT:
As Mike noticed this is not exactly the same code as what you are feeding to the compiler. Is the constructor/destructor public or is Class a struct?
However google for C++ most vexing parse.
You can use it like this:
Class obj;
//or
Class *obj = new Class(/*constructor arguments*/);

"class" Keyword on Return Type - C++ [duplicate]

This question already has answers here:
what is 'class' in 'class DataType* Variable' in Unreal Engine Shooter Game Sample
(3 answers)
Closed 7 years ago.
I came across a piece of code that looked like this:
class SomeClass* GetSomeClass()
{
return _instanceOfSomeClass;
}
What does the "class" keyword do on the return type? I can't find anywhere that explains what it's function is. Does it just specify that it's talking about SomeClass as a class in case there is some sort of ambiguousness or something? I am confused.
class SomeClass is a longhand way of referring to the class type SomeClass (technically, it's the elaborated type specifier). Usually, adding class is redundant, and the two are equivalent. But it's sometimes necessary to resolve the ambiguity, if there's a variable or function with the same name.
It is used to disambiguate.
Say for example if you have a variable of the same name in the same (or outer) scope, something like this:
int SomeClass; //SomeClass is declared to be variable here
class SomeClass* GetSomeClass()
{
return _instanceOfSomeClass;
}
Without the class keyword, the function declaration wouldn't make sense to the compiler. The class keyword tells the compiler to ignore the variable declaration, and look for a class declaration.
It's a forward declaration. It allows you to just say "there is a class SomeClass somewhere in my program, it is just not visible to this file in order to prevent redeclerations".
Whenever you implement this function, though, the file must have actual interface of class SomeClass.

C++ Struct internal declaration confusion? [duplicate]

This question already has an answer here:
Factory Pattern: typedef Class *(createClassFunction)(void)
(1 answer)
Closed 8 years ago.
I've come across a declaration inside a C++ Struct{..} that I've never seen before.
Can anyone tell me what it means;
struct DerivedMesh {
char cd_flag;
void (*calcNormals)(DerivedMesh *dm); // <-- What is this?
It kind of looks like it's dereferencing a pointer called calcNormals, but that's all I can make out.
This is a C syntax for declaring function pointers.
In this particular example, DerivedMesh will have a member calcNormals that is a pointer to a function accepting single argument of type DerivedMesh*. It can be called like an ordinary function:
void foo(DerivedMesh* dm) { ... }
DerivedMesh dm;;
// Init members and set calcNormals to actual function
dm.cf_flag = whatever;
dm.calcNormals = foo;
dm.calcNormals(&dm); // calls foo
This
void (*calcNormals)(DerivedMesh *dm);
is class data member definition with name calcNormals that has type of pointer to function of type void( DerivedMesh * )

Why is there no call to the constructor? [duplicate]

This question already has answers here:
Default constructor with empty brackets
(9 answers)
Closed 4 years ago.
This code doesn't behave how I expect it to.
#include<iostream>
using namespace std;
class Class
{
Class()
{
cout<<"default constructor called";
}
~Class()
{
cout<<"destrutor called";
}
};
int main()
{
Class object();
}
I expected the output 'default constructor called', but I did not see anything as the output. What is the problem?
Nope. Your line Class object(); Declared a function. What you want to write is Class object;
Try it out.
You may also be interested in the most vexing parse (as others have noted). A great example is in Effective STL Item 6 on page 33. (In 12th printing, September 2009.) Specifically the example at the top of page 35 is what you did, and it explains why the parser handles it as a function declaration.
No call to constructor
Because the constructor never gets called actually.
Class object(); is interpreted as the declaration of a function object taking no argument and returning an object of Class [by value]
Try Class object;
EDIT:
As Mike noticed this is not exactly the same code as what you are feeding to the compiler. Is the constructor/destructor public or is Class a struct?
However google for C++ most vexing parse.
You can use it like this:
Class obj;
//or
Class *obj = new Class(/*constructor arguments*/);