Re: initializing a class using upcasted object
The misuse is caused by the Der1 object created by the statement: "X
x1(Der1(4,6));" destroyed immediately after used as a parameter to
construct x1. So the solution here is to have this anonymous Der1
object live longer, one way to do is to have the X object make a copy
of it.
class Base {
public:
Base() {}
virtual ~Base() {}
virtual void foo() const =0;
};
class Der1 : public Base {
private:
string mname;
public:
Der1(int a,int b, string name=""):mname(name),Base() { }
~Der1() {cout<<endl<<"destroyed "<<mname;}
virtual void foo() const { cout<<"der1";}
};
template
<typename DerType>
class X {
private:
const DerType ref;
public:
X(const DerType& ref):ref(ref) {}
~X() {}
void complicated_function_that_uses_foo()
{
ref.foo();
};
};
//int _tmain(int argc, _TCHAR* argv[])
int main()
{
Der1 temp(2,4, "normal use");
X<Der1> x(temp);
X<Der1> x1(Der1(4,6, "incorrect use")); // no error in gcc,
// tom: no error comfirmmed in vs c++, but after
// xl is constructed successfully, the object from
// Der1(4,6, "incorrect use") is destroyed immediately,
you will see this by
// run this
program and debug it step by step
x.complicated_function_that_uses_foo();
x1.complicated_function_that_uses_foo(); //here the progam
// exits with "pure virtual method called"
return 0;
}
class Base {
public:
Base() {}
virtual ~Base() {}
virtual void foo() const =0;
};
class Der1 : public Base {
private:
string mname;
public:
Der1(int a,int b, string name=""):mname(name),Base() { }
~Der1() {cout<<endl<<"destroyed "<<mname;}
virtual void foo() const { cout<<"der1";}
};
template
<typename DerType>
class X {
private:
const DerType ref;
public:
X(const DerType ref):ref(ref) {}
~X() {}
void complicated_function_that_uses_foo()
{
ref.foo();
};
};
//int _tmain(int argc, _TCHAR* argv[])
int main()
{
Der1 temp(2,4, "normal use");
X<Der1> x(temp);
X<Der1> x1(Der1(4,6, "incorrect use")); // no error in gcc,
// tom: comfirmmed in vs c++, but after
// xl is constructed successfully, the object from
// Der1(4,6, "incorrect use") is destroyed immediately
x.complicated_function_that_uses_foo();
x1.complicated_function_that_uses_foo(); //here the progam
// exits with "pure virtual method called"
return 0;
}
On Jul 22, 6:50 pm, mati <longrai...@gazeta.NOSPAM.pl> wrote:
tom wrote:
I don't know what you meant. I get your code compiled, and found that
the misused scenario you pointed out actually doesn't pass the
compilation, see the comment in the code below:
You're right. It does not compile at gcc too.
I've cut too much code before posting, anyway, here is the code (a minor
modification of yours) that compiles, but is wrong, could you test it
please? And is it a bug in gcc, or I'm missing something? (probably the
latter...)
#include <iostream>
using namespace std;
class Base {
public:
Base() {}
virtual ~Base() {}
virtual void foo() const =0;
};
class Der1 : public Base {
public:
Der1(int a,int b):Base() { }
~Der1() {}
virtual void foo() const { cout<<"der1";}
};
class X {
const Base& ref;
public:
X(const Base& ref):ref(ref) {}
~X() {}
void complicated_function_that_uses_foo()
{
ref.foo();
};
};
//int _tmain(int argc, _TCHAR* argv[])
int main()
{
Der1 temp(2,4);
X x(temp);
X x1(Der1(4,6)); // no error in gcc
x.complicated_function_that_uses_foo();
x1.complicated_function_that_uses_foo(); //here the progam
// exits with "pure virtual method called"
return 0;
}
#include <iostream>
using namespace std;
class Base {
public:
Base() {}
virtual ~Base() {}
virtual void foo() const =0;
};
class Der1 : public Base {
public:
Der1():Base() { }
~Der1() {}
virtual void foo() const { cout<<"der1";}
};
class X {
const Base& ref;
public:
X(const Base& ref):ref(ref) {}
~X() {}
void complicated_function_that_uses_foo()
{
ref.foo();
};
};
int _tmain(int argc, _TCHAR* argv[])
{
Der1 temp;
X x(temp);
//X x(Der1()); //error: this line can't be compiled,
//in vs I got an error: C4930
x.complicated_function_that_uses_foo();
return 0;
}
--
mati- Hide quoted text -
- Show quoted text -