What does :className() mean in a constructor for className? - c++

I see some code in a codebase I'm working on that looks like:
ZfooName::ZfooName(int magoo)
: ZfooName()
{
fGoo = magoo;
}
I'm assuming this is a C++11 feature, since it breaks in VS2012, but what does it mean?

This is a new feature in C++11. It's called a delegating constructor.
The constructor calls the default constructor first (the constructor that is being delegated to). After the default constructor returns, the body of the delegating constructor is executed.
See http://www.stroustrup.com/C++11FAQ.html#delegating-ctor and https://en.cppreference.com/w/cpp/language/initializer_list#Delegating_constructor for additional information.

Related

Compiler generated move constructor behavior in VC12 (VS2013) [duplicate]

This question already has an answer here:
Default Move Constructor in Visual Studio 2013 (Update 3)
(1 answer)
Closed 7 years ago.
I was trying to test the behavior of compiler generated Move Constructor for a class having data members as "std::string" and "std::shared_ptr".
class Record
{
public:
Record():mData("defRec"), mDataPtr(new std::string("defRec"))
{}
Record(const std::string &str): mData(str), mDataPtr(new std::string(str))
{}
private:
std::string mData;
std::shared_ptr<std::string> mDataPtr;
};
Any D'tor, move and copy operations have been deliberately omitted to allow compiler to generate Move operations. The class data members have especially been chosen since unlike the Record class, they both have D'tor, move and copy operations defined.
After the following code,
Record rec1(std::string("Record1"));
Record rec2(std::move(rec1));
on inspecting object rec1 (via debugger or by defining access methods), mData still contains "Record1" and mDataPtr still holds the string pointer.
However, If I provide my own Move Constructor as below
Record(Record&& rhs) : mData(std::move(rhs.mData)), mDataPtr(std::move(rhs.mDataPtr))
{}
then after the following code
Record rec1(std::string("Record1"));
Record rec2(std::move(rec1));
on inspecting object rec1, both mData and mDataPtr become empty object.
I don't understand why the compiler generated move constructor for "class Record" gives different result to my version of move constructor for "class Record" where I just ensure the move C'tor for data members to be invoked. I would expect the same behavior for compiler generated move constructor?
Though the moved from object is supposed to be in a valid state, the data members' move constructor (not defined by me on both occasions) gives different results.
Is this a known limitation of VC12 (VS2013)?
Yes this is a known limitation of that compiler. From Can a move constructor be implicit? the question's EDIT EDIT shows us:
EDIT EDIT I pinged Stephan T Lavavej and asked him why VC 2012 doesn't
seem to follow what draft 12.8 states regarding implicit move
constructor generation. He was kind enough to explain:
It's more of a "feature not yet implemented" than a bug. VC currently implements what I refer to as rvalue references v2.0, where
move ctors/assigns are never implicitly generated and never affect the
implicit generation of copy ctors/assigns. C++11 specifies rvalue
references v3.0, which are the rules you're looking at.
In short the move constructor is never being generated by the compiler and the copy constructor is called instead even in the presence of the explicit std::move.
According to the standard, there are a bunch of reasons the compiler wouldn't be required to generate a default move constructor but your class doesn't appear to violate any of them.

Object gets constructed from return value without copy or move constructor

I've got an Action class that looks like this (in its stripped down form):
struct Action {
explicit Action(...some parameters...); // I only use this to construct Action objects
Action(const Action&) = delete; // Don't want copy constructor
Action(Action&&) = delete; // Same for move constructor
}
In some other translation unit, I have tried to do this:
Action action = someMethodForGettingActions(); // The method returns Action objects by rvalue
Visual Studio's Intellisense wants to hang me for this, justifiably. It says it can't access the move constructor.
Yet this compiles and runs as expected. What's going on here? Is this some compiler optimization playing mind tricks on me?
It is a Return Value Optimization at work.
It is actually permitted by C++11 standard. Check the accepted answer of:
c++11 Return value optimization or move?
The question there was a bit different, but the answer fits your problem.

Compiler generated methods

When declaring a class/struct/union the compiler will generate the default methods (rule of three). This also will happen when = default'ing these methods.
How do the default methods exactly look like?
For each of these methods, the compiler defines default inline methods that call the default methods of each attribute of the object. (so a pointer won't be initialized neither any built-in type).
For example let consider the default constructor. According to the C++ Standard
An implicitly-declared default constructor is an inline public member
of its class.
and
The implicitly-defined default constructor performs the set of
initializations of the class that would be performed by a user-written
default constructor for that class with no ctor-initializer (12.6.2)
and an empty compound-statement.
So it looks like
struct A
{
A() {}
};
except that it is not explicitly declared and defined.
About copy constructor you can read at my personal forum
http://cpp.forum24.ru/?1-1-0-00000021-000-0-0-1388485669
Though it is written in Russian but you can translate it for example using google service translate.
Those method will do the minimum necessary to initialize the class.
Default construtor - do nothing with simple members but call the constructors of more complex members (class/struct) as well as call the ctor of its super class.
Copy constructor will perform a shallow copy (memcpy).

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.

Why does IntelliSense show a constructor I didn't write?

What does it mean by the suggestion give by the IDE? (using VS 2010)
There is no constructor of baseClass_2 which takes the suggested parameter(const baseClass_2 &). So, why this is showing up?
Implicitly defined copy constructor.
There are special member functions which get defined by default when you not explicitly declare / define them:
Default constructor
Copy constructor
Destructor
Note that the default constructor will not get defined when you provide any other constructor besides the copy constructor.
The constructor is the copy constructor and it is declared and defined implicitly.