A Few Questions

M

Michael

Hi,

I have a few questions here.

1. Is there a way to go from the sub-classes (one or more layers) to
the base class directly?

2. What's the sequence of executing constructors function and
destructor function of base class and subclasses?

3. Can a function name starting with $, #, !, -, or a number?

Thanks a lot in advance!
Michael
 
N

Noah Roberts

Michael said:
Hi,

I have a few questions here.

1. Is there a way to go from the sub-classes (one or more layers) to
the base class directly?

Don't know what you mean.
2. What's the sequence of executing constructors function and
destructor function of base class and subclasses?

Classes are constructed from most base to most derived, they are
destroyed in the opposite order.
3. Can a function name starting with $, #, !, -, or a number?

No.
 
V

Victor Bazarov

Michael said:
1. Is there a way to go from the sub-classes (one or more layers) to
the base class directly?

It depends on what you mean by "go". Conversions from a class to any
of its bases exist if the base is unambiguous and accessible. Sometimes
you need to specify the path the compiler must take to reach the base.
2. What's the sequence of executing constructors function and
destructor function of base class and subclasses?

The order is the same as the declaration order. Constructors are invoked
in that order. Destructors are invoked in reverse to constructors.
3. Can a function name starting with $, #, !, -, or a number?

Come on! Just peek in your damn textbook.

V
 
R

Robert J. Hansen

1. Is there a way to go from the sub-classes (one or more layers) to
the base class directly?

A couple of them, in fact. The following code has been tested with G++
4.0.1; other compilers will probably give comparable results.

=====
#include <iostream>

struct Foo {
void print() { std::cout << "Foo" << std::endl; }
};

struct Bar : public Foo {
void print() { std::cout << "Bar" << std::endl; }
};

struct Baz : public Bar {
// This also works
// void print() { Foo::print(); }
void print() { static_cast<Foo* const>(this)->print(); }
};

int main()
{
Baz b;
b.print();
return 0;
}
=====

Please note that in the Baz class, it's usually considered highly
idiosyncratic to use a static_cast to move up in the inheritance
hierarchy. That said, it's entirely legal C++.

Using the scope resolution operator :):) is generally the preferred
solution.
2. What's the sequence of executing constructors function and
destructor function of base class and subclasses?

Top-to-bottom for ctors; bottom-up for dtors.
3. Can a function name starting with $, #, !, -, or a number?

No.
 
M

Michael

Victor said:
It depends on what you mean by "go". Conversions from a class to any
of its bases exist if the base is unambiguous and accessible. Sometimes
you need to specify the path the compiler must take to reach the base.

I mean is there a function called parent() or base() to go from
subclass to base class? I do not think so. But I need a confirmation.

Thanks,
 
V

Victor Bazarov

Michael said:
I mean is there a function called parent() or base() to go from
subclass to base class? I do not think so. But I need a confirmation.

No, there is not, unless you provide your own.

V
 
N

Noah Roberts

Michael said:
I mean is there a function called parent() or base() to go from
subclass to base class? I do not think so. But I need a confirmation.

C++ has multiple inheritance so there is no "super" pointer or function
since there can be more than one. Unfortunately you have to know which
base to call, which makes changing an object higherarchy more difficult
at times. Of course you could always typedef one:

class X : public B
{
typedef B super;
public:
void f() { super::f(); ...; }
};
 
F

Frederick Gotham

Michael posted:
1. Is there a way to go from the sub-classes (one or more layers) to
the base class directly?


Base &base = derived_object;

Base *pbase = &derived_object;

2. What's the sequence of executing constructors function and
destructor function of base class and subclasses?


Guess.

(Here's a clue: Phones were invented before cellphones.)

3. Can a function name starting with $, #, !, -, or a number?


Try to compile this:

int main()
{
int $a;
int #a;
int !a;
int -a;
}
 

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

Similar Threads

few questions concerning classes 7
A few minor questions 30
A Few C++ Questions 4
A few function questions 6
a few questions about scrapy 0
A few (advanced?) questions 6
A few VHDL questions 3
A few questions on C++ 43

Members online

Forum statistics

Threads
473,744
Messages
2,569,481
Members
44,900
Latest member
Nell636132

Latest Threads

Top