Z
zeppe
Hi all,
I've a problem. The code that follows creates a warning in both gcc and
visual c++. However, I think it's correct: basically, there is a
function that return an object of a derived class, that's bounded to a
base class reference to delay the destruction of the actual object to
the end of the reference scope. Actually, I don't use the reference: the
code that matters is in the destructor, and I want it to be executed at
the end of the method.
Really strange, both the compilers warn me about the "useless"
reference... I'm feared that if the compilers "think" that the reference
is useless, they could try some dangerous optimization...
Here follows a simple example:
#include <iostream>
class Base
{
public:
Base() { std::cout << "Base\n"; std::cout.flush(); }
virtual ~Base() { std::cout << "~Base\n"; std::cout.flush(); }
virtual char* Name() const { return "Base"; }
};
class Derived : public Base
{
public:
Derived() { std::cout << "Derived\n"; std::cout.flush(); }
~Derived() { std::cout << "~Derived\n"; std::cout.flush(); }
char* Name() const { return "Derived"; }
};
Derived foo()
{
std::cout << "foo function\n"; std::cout.flush();
return Derived();
}
int main()
{
std::cout << "begin of the program\n"; std::cout.flush();
const Base& obj = foo();
std::cout << "this is the program body\n"; std::cout.flush();
return 0;
}
Thank you,
Zeppe
I've a problem. The code that follows creates a warning in both gcc and
visual c++. However, I think it's correct: basically, there is a
function that return an object of a derived class, that's bounded to a
base class reference to delay the destruction of the actual object to
the end of the reference scope. Actually, I don't use the reference: the
code that matters is in the destructor, and I want it to be executed at
the end of the method.
Really strange, both the compilers warn me about the "useless"
reference... I'm feared that if the compilers "think" that the reference
is useless, they could try some dangerous optimization...
Here follows a simple example:
#include <iostream>
class Base
{
public:
Base() { std::cout << "Base\n"; std::cout.flush(); }
virtual ~Base() { std::cout << "~Base\n"; std::cout.flush(); }
virtual char* Name() const { return "Base"; }
};
class Derived : public Base
{
public:
Derived() { std::cout << "Derived\n"; std::cout.flush(); }
~Derived() { std::cout << "~Derived\n"; std::cout.flush(); }
char* Name() const { return "Derived"; }
};
Derived foo()
{
std::cout << "foo function\n"; std::cout.flush();
return Derived();
}
int main()
{
std::cout << "begin of the program\n"; std::cout.flush();
const Base& obj = foo();
std::cout << "this is the program body\n"; std::cout.flush();
return 0;
}
Thank you,
Zeppe