Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to assign / retrieve base class?

Suppose I have:

class Foo {
  ...
};
class Bar : public Foo {
  ...
};
Foo foo;
Bar bar;

Is there anyway to do the following:

foo_part_of_bar(bar) = foo;

foo = foo_part_of_bar(bar);

?

Thanks!

like image 341
anon Avatar asked Jan 23 '23 12:01

anon


1 Answers

Assuming you meant class Bar : public Foo, the following should work.

For foo_part_of_bar(bar) = foo;

*(static_cast<Foo *>(&bar)) = foo;

For foo = foo_part_of_bar(bar);

foo = bar;
like image 143
R Samuel Klatchko Avatar answered Jan 25 '23 01:01

R Samuel Klatchko