newbie and polymorphism

  • Thread starter Vincent RICHOMME
  • Start date
V

Vincent RICHOMME

Hi,

I wrote this code and I thinks it's really bad to do it but I would like
a confirmation :

class A
{
public:

void Print()
{
std::cout << "A::print()" << std::endl;
}
};


class DerivA : public A
{
public:

void Print()
{
std::cout << "derivA::print()" << std::endl;
}

};


int main()
{
//std::cout << "Hello world!" << std::endl;

A* pA = new A();
DerivA* pDerivA = (DerivA*) pA;

pDerivA->Print();


return 0;
}

Actually I cast tha base class pA to a derived one, and I don't think
it's allowed.
 
R

Rolf Magnus

Vincent said:
Hi,

I wrote this code and I thinks it's really bad to do it but I would like
a confirmation :

class A
{
public:

void Print()
{
std::cout << "A::print()" << std::endl;
}
};


class DerivA : public A
{
public:

void Print()
{
std::cout << "derivA::print()" << std::endl;
}

};


int main()
{
//std::cout << "Hello world!" << std::endl;

A* pA = new A();
DerivA* pDerivA = (DerivA*) pA;

pDerivA->Print();


return 0;
}

Actually I cast tha base class pA to a derived one, and I don't think
it's allowed.

You are right. You should use polymorphism instead.

Btw: If casting, you should stick with C++ style casts.
 
N

Neelesh Bodas

Vincent said:
Hi,

I wrote this code and I thinks it's really bad to do it but I would like
a confirmation :

class A
{
public:

void Print()
{
std::cout << "A::print()" << std::endl;
}
};


class DerivA : public A
{
public:

void Print()
{
std::cout << "derivA::print()" << std::endl;
}

};


int main()
{
//std::cout << "Hello world!" << std::endl;

A* pA = new A();
DerivA* pDerivA = (DerivA*) pA;

pDerivA->Print();


return 0;
}

Actually I cast tha base class pA to a derived one, and I don't think
it's allowed.

Well, to be exact, it is _allowed_. Its a different matter that it is a
"bad practice". But there arise situations where one needs to downcast
a base class pointer to a derived class pointer. C++ defines a specific
cast called dynamic_cast for this.

pDerivA = dynamic_cast<DerivA*>(pA);

Note that this code doesnot compile in the current scenario since base
class is not polymorphic (It has no virtual functions). But then it
should not be subclassed at the first place.

Also, the class to Print() is statically bound.
 
F

Frinos

Vincent said:
Hi,

I wrote this code and I thinks it's really bad to do it but I would like
a confirmation :

class A
{
public:

void Print()
{
std::cout << "A::print()" << std::endl;
}
};

OK


class DerivA : public A
{
public:

void Print()
{
std::cout << "derivA::print()" << std::endl;
}

};


OK

int main()
{
//std::cout << "Hello world!" << std::endl;

A* pA = new A();
DerivA* pDerivA = (DerivA*) pA;

pDerivA->Print();


return 0;
}

Actually I cast tha base class pA to a derived one, and I don't think
it's allowed.

It is allowed.... Any type can be cast to any other type and the code
may compile BUT whether it runs correctly is a different story... and
yup it is really bad to do...
 
D

Dietmar Kuehl

Frinos said:
It is allowed....

The C++ standard does not speak in terms of permissions, i.e. I'm not
clear what either of you means with "allowed". The standard speaks in
terms of defined behavior and whether a compiler is supposed to issue
a diagnostic.

The code is supposed to be compiled without a diagnostic. In this
sense it is "allowed" C++ code as the compiler is required to compile
it without an error. However, the standard is explicit about whether
the semantics of the resulting program are defined or not: they are
not. In this sense it is "not allowed" C++ code because you cannot
predict the behavior when running the program: it is free to do
whatever the implementer of the C++ compiler thought reasonable or
it may do whatever it just happens to do by pure coincidence.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,778
Messages
2,569,605
Members
45,237
Latest member
AvivMNS

Latest Threads

Top