Order of return values from functions - c++

Here is my code in C++:
MyClass foo1() {
return MyClass();
}
int MyClass::foo2() {
return 54;
}
And the question is, what is the value of:
foo1().foo2();
Is the value 54 or is it the MyClass object?

Since the dot . operator is evaluated left-to-right (i.e. first foo1() is called to determine the target of the invocation, and then its foo2() member is invoked), the answer is 54.

foo1().foo2(); means that foo1() returns an object or reference to an object that allows you to call foo2() on it.
Similar would be to store the return value of foo1() in a variable:
MyClass obj = foo1();
int ret = obj.foo2();
Example (lazy-loaded singleton):
class MyClass
{
public:
static MyClass& getInstance(){ static MyClass m; return m; }
int getNumber(){ return 54; }
};
int main()
{
std::cout << MyClass::getInstance().getNumber();
}

Related

Is there a better/safe way to cast non-const reference of shared_ptr to a base class?

If you have a class Base with virtual methods and a class Implementation which implements the virtual methods, is there any way to cast std::shared_ptr < Implementation > & to std::shared < Base > &? The compiler allows this for const references, but for non const references it fails as in "Case A" in the code below. Is there an easy way to do this?
If not, how safe is my workaround "questionable_cast" in Case B?
#include <iostream>
#include <memory>
class Base
{
public:
virtual void set_value(int x) = 0;
};
class Implementation : public Base
{
public:
Implementation() : m_value(0) { }
void set_value(int x) override
{
m_value = x;
}
int get_value() const
{
return m_value;
}
private:
int m_value;
};
void do_something(std::shared_ptr<Base>& base)
{
base->set_value(5);
/// Code like this makes the non-const argument necessary
base = std::make_shared<Implementation>();
}
template <class T, class U>
std::shared_ptr<T>& questionable_cast(std::shared_ptr<U>& u)
{
/// This code is here to assure the cast is allowed
std::shared_ptr<T> tmp = u;
(void)tmp;
return *reinterpret_cast<std::shared_ptr<T>*>(&u);
}
int main()
{
std::shared_ptr<Implementation> a = std::make_shared<Implementation>();
// The following line causes a compiler error:
// invalid initialization of reference of type ‘std::shared_ptr<Base>&’ ...
// do_something(a);
// do_something(std::dynamic_pointer_cast<Base>(a));
// This is the workaround
do_something(questionable_cast<Base>(a));
std::cerr << "a = " << a->get_value() << std::endl;
return 0;
}
Two obvious solutions to the problem as originally asked: 1. Make do_something take a const reference to a shared_ptr (or a shared_ptr by value). 2. Create a named shared_ptr and pass a reference to that: Eg
int main()
{
std::shared_ptr<Implementation> a = std::make_shared<Implementation>();
std::shared_ptr<Base> b = a; // This conversion works.
do_something(b); // Pass a reference to b instead.
return 0;
}
Your questionable_cast function is a violation of the strict aliasing rules, and invokes undefined behaviour. It's quite likely to work in initial tests, and then a new release of the compiler will crank up the optimization a notch, and it will fail during a demo.
To handle the case where do_something changes the pointer:
int main()
{
std::shared_ptr<Implementation> a = std::make_shared<Implementation>();
std::shared_ptr<Base> b = a; // This conversion works.
do_something(b); // Pass a reference to b instead.
const auto aa = std::dynamic_pointer_cast<Implementation>(b);
if (aa)
a = aa;
else
; // Handle the error here
return 0;
}
If do_something guarantees to return a pointer of the same derived type, even if it doesn't return the same pointer, wrap it in a template function:
template <typename T>
void do_something_ex( std::shared_ptr<T>& a )
{
std::shared_ptr<Base> b = a;
do_something(b)
a = std::dynamic_pointer_cast<T>(b);
if (!a)
throw_or_assert;
}

error on reference to return value

This code can't pass compiling. The only difference is the return type. Foo1's return type is the user-defined struct and Foo2's is int.
struct test
{
};
test Foo1()
{
return test();
}
int Foo2()
{
return 0;
}
int main()
{
test& r1 = Foo1(); //ok
int& r2 = Foo2(); //no but why? Is it the C++ standard way?
return 0;
}
It is either a compiler bug or its "language extension" (for example MS VC++ has many such "language extensions"). In both cases of the function calls the compiler shall issue an error because it may not bind a temporary object to non-const reference.
If you want to have a reference you can still use a reference to const otherwise neither of the line will compile :
struct test
{
};
test Foo1()
{
return test();
}
int Foo2()
{
return 0;
}
int main()
{
const test& r1 = Foo1(); // ok now
const int& r2 = Foo2(); //ok now
return 0;
}

About const combined with reference and virtual inheritance

I have the following code:
class A
{
int x;
public:
A(int i=25) { x=i; }
int& f() const { return x; }
};
int main()
{
A a(15);
cout<<a.f();
return 0;
}
I get an error saying that I can't initialize a reference of type int& from an expression of type const int? If I modify function's f() header, like "int f() const" or like "int& f()" it works. I know that a const function can't modify the members of the class, but in my example that's not happening and still is something wrong I can't understand.
Secondly, I have this piece of code:
class B { /* instructions */ };
class D1: virtual B { /* instructions */ };
class D2: virtual B { /* instructions */ };
class D3: B { /* instructions */ };
class D4: private B { /* instructions */ };
class D5: virtual public B { /* instructions */ };
class M1: D1, public D2, D3, private D4, virtual D5
{ /* instructions */ };
class M2: D1, D2, virtual D3, virtual D4, virtual D5
{ /* instructions */ };
The question is how many times is B inherited in M1 and and in M2. I think the answer is 3 (first from D1, then from D3 and then from D4) for both, but I'm not sure. Am I right?
You're returning a reference to a member of your class. However, since you method is const, you're not allowed to return a non-const reference, because this would allow for the end user to modify your class state, after calling a const method.
Consider this:
class A
{
int x;
public:
A(int i=25) { x=i; }
const int& f() const { return x; }
};
int main()
{
A a(15);
int &ref = const_cast<int &>(a.f());
ref = 42;
// a.x == 42
return 0;
}
Using const_cast is generally a bad idea, it's here to demonstrate what could happen if you were allowed to return a non-const reference from a const method.
The end user could change the property x from your class, because of a call to a const function, which is not supposed to happen.
Returning the result by value is imo the way to go, since it's just an integer.
First, your function may not be modifying a const value, but you're trying to return a const value as a non-const, which could then be modified. The compiler is protecting you. To keep it const, modify it as:
const int &f() const {... }
or
int f() const { .... }
The first returns a const reference, the second returns the result by-value.
In a non-static const member function, the type of this is changed to A const *const. Therefore it follows that the type of x is changed to int const&. A const object cannot be implicitly-converted to a non-const version of its type.
For your first question:
class A
{
int x;
public:
A(int i=25) { x=i; }
const int& f() const { return x; }
//^^You should add const here
};
int main()
{
A a(15);
cout<<a.f();
return 0;
}
Otherwise, you are returning non-const reference from a const member function. Another option is to simply return by value.
const int& f() const is the way.
You're right, 3 times for both.

C++ - Issue of using function pointer to member function

I've applied solutions based on some search made, but the problem still there. Thank you so much for the help.
error: must use '.*' or '->*' to call pointer-to-member function ...
source code:
#include <stdio.h>
class A
{
public:
struct data;
typedef int (A::*func_t)(data *);
typedef struct data
{
int i;
func_t func;
}
data;
data d;
void process()
{
d.func(&d);
}
A()
{
d.i = 999;
d.func = &A::print;
}
int print(data *d)
{
printf("%d\n", d->i);
return 0;
}
};
int main()
{
A *a = new A;
a->process();
return 0;
}
d.func(&d);
is not enough. func is a member-function-pointer which is pointing to a non-static member of A. So it can be invoked on an object of A. So you need to write this:
(this->*(d.func))(&d);
That would work as long as you write this inside A.
If you want to execute func from outside, say in main(), then the syntax is this:
A a;
(a.*(a.d.func))(&a.d);
That is an ugly syntax.
Your process function attempts to call d.func but it is a pointer to member function. A pointer to member function must be called on some object. Presumably you want the instance of A to be this, in which case your process function should look like:
void process()
{
(this->*(d.func))(&d);
}
Note the use of the ->* operator to call a member function when you have a pointer to it.
Other answers have already said you need to say (this->*d.func)(&d) to call a pointer-to-member function (because you need to provide the object that it's a member of)
Another option is to make the function a static function, which doesn't need special syntax to call. To do that, change the typedef like so:
typedef int (*func_t)(data *);
Then make the print function static:
static int print(data *d)
{
...
}
Now you can just call d.func(&d)
Unfortunately what you are trying to do will not be possible, the reason being that print is not a static member function. This means it expects an implicit first argument that is the this pointer.
I suggest you try using the std::function and std::bind function, something like this:
class A
{
struct data
{
std::function<void(const data&)> func;
int i;
};
data d;
public:
A()
{
d.func = std::bind(&A::print, *this);
d.i = 999;
}
void process()
{
d.func(d);
}
void print(const data& my_data)
{
std::cout << my_data.i << '\n';
}
};
Of course, since the print function now have a proper this pointer, you no longer need to pass the data structure to it:
class A
{
struct data
{
std::function<void()> func;
int i;
};
data d;
public:
A()
{
d.func = std::bind(&A::print, *this);
d.i = 999;
}
void process()
{
d.func();
}
void print()
{
std::cout << d.i << '\n';
}
};
Calling pointer the members require the class it is a member of to be the this param.
Try:
A a;
a.*(d.func)(&d);

C++: Array of member function pointers to different functions

I have a class A which contains member functions foo() and bar() which both return a pointer to class B. How can I declare an array containing the functions foo and bar as a member variable in class A? And how do I call the functions through the array?
The member function pointer syntax is ReturnType (Class::*)(ParameterTypes...), so e.g.:
typedef B* (A::*MemFuncPtr)(); // readability
MemFuncPtr mfs[] = { &A::foo, &A::bar }; // declaring and initializing the array
B* bptr1 = (pointerToA->*mfs[0])(); // call A::foo() through pointer to A
B* bptr2 = (instanceOfA.*mfs[0])(); // call A::foo() through instance of A
See e.g. this InformIT article for more details on pointers to members.
You might also want to look into Boost.Bind and Boost.Function (or their TR1 equivalents) which allow you to opaquely bind the member-function-pointers to an instance:
typedef boost::function<B* ()> BoundMemFunc;
A instanceOfA;
BoundMemFunc mfs[] = {
boost::bind(&A::foo, &instanceOfA),
boost::bind(&A::bar, &instanceOfA)
};
B* bptr = mfs[0](); // call A::foo() on instanceOfA
To use such an array as a member, note that you can't initialize arrays using the member initializer list. Thus you can either assign to it in the constructor body:
A::A {
mfs[0] = &A::foo;
}
... or you use a type that can actually be initialized there like std::vector or boost::array:
struct A {
const std::vector<MemFuncPtr> mfs;
// ...
};
namespace {
std::vector<MemFuncPtr> init_mfs() {
std::vector<MemFuncPtr> mfs;
mfs.push_back(&A::foo);
mfs.push_back(&A::bar);
return mfs;
}
}
A::A() : mfs(init_mfs()) {}
What you're looking for are pointers to member functions. Here is a short sample that shows their declaration and use:
#include <iostream>
class B {
public:
B(int foo): foo_(foo) {
std::cout << "Making a B with foo_ = " << foo_ << std::endl;
}
~B(void) {
std::cout << "Deleting a B with foo_ = " << foo_ << std::endl;
}
int foo_;
};
class A {
public:
A(void) {
funcs_[0] = &A::foo;
funcs_[1] = &A::bar;
}
B* foo(void) {
return new B(3);
}
B* bar(void) {
return new B(5);
}
// Typedef for the member function pointer, for everyone's sanity.
typedef B* (A::*BMemFun)(void);
BMemFun funcs_[2];
};
int main(int argc, char *argv[]) {
A a;
for (int i = 0; i < 2; ++i) {
A::BMemFun func = a.funcs_[i];
// Call through the member function pointer (the .* operator).
B* b = (a.*func)();
delete b;
}
return 0;
}
The C++ FAQ section on pointers to member functions is where I found all this information.
C++ that's not ancient (read: C++11 and later) makes this all easier. In modern C++, you can do
#include <vector>
class B;
class A {
public:
B* foo() {
// return something;
return nullptr;
}
B* bar() {
// return something;
return nullptr;
}
//C++ 11: functional brings std::function, which has zero overhead
//but is actually a useful type with which one can work
std::vector<std::function<B*()>> container;
/* [=]() { return foo(); }
* that's a lambda. In practice it "compiles away", i.e. calling
* the lambda function is the same as calling foo or bar directly
* Note how [=] means we're passing in "this", so that we can
* actually call foo().
*/
A() : container{{[=]() { return foo(); }}, {[=]() { return bar(); }}} {}
};
(Try on godbolt compiler explorer)
Here's a more complete example showcasing what to do with these.
An architectural remark: Be careful with pointers to non-static member functions. What happens if your instance of A gets destroyed, but you still have a function handle to a member function? Right, hell freezes over: There's no object anymore to which this method belongs, so results are catastrophic.