473,396 Members | 1,816 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

A Few Questions

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

Jul 31 '06 #1
7 1005

Michael wrote:
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.

Jul 31 '06 #2
Michael wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 31 '06 #3
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.

Jul 31 '06 #4

Victor Bazarov wrote:
Michael wrote:
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.
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,

Jul 31 '06 #5
Michael wrote:
Victor Bazarov wrote:
>Michael wrote:
>>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.

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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 31 '06 #6

Michael wrote:
Victor Bazarov wrote:
Michael wrote:
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.

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(); ...; }
};

Jul 31 '06 #7
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;
}

--

Frederick Gotham
Jul 31 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: softwareengineer2006 | last post by:
All Interview Questions And Answers 10000 Interview Questions And Answers(C,C++,JAVA,DOTNET,Oracle,SAP) I have listed over 10000 interview questions asked in interview/placement test papers for...
0
by: connectrajesh | last post by:
INTERVIEWINFO.NET http://www.interviewinfo.net FREE WEB SITE AND SERVICE FOR JOB SEEKERS /FRESH GRADUATES NO ADVERTISEMENT
2
by: freepdfforjobs | last post by:
Full eBook with 4000 C#, JAVA,.NET and SQL Server Interview questions http://www.questpond.com/SampleInterviewQuestionBook.zip Download the JAVA , .NET and SQL Server interview sheet and rate...
4
by: Drew | last post by:
I posted this to the asp.db group, but it doesn't look like there is much activity on there, also I noticed that there are a bunch of posts on here pertaining to database and asp. Sorry for...
8
by: Krypto | last post by:
Hi, I have used Python for a couple of projects last year and I found it extremely useful. I could write two middle size projects in 2-3 months (part time). Right now I am a bit rusty and trying...
0
by: ramu | last post by:
C# Interview Questions and Answers8 http://allinterviewsbooks.blogspot.com/2008/07/c-interview-questions-and-answers8.html C# Interview Questions and Answers7...
1
by: ramu | last post by:
C# Interview Questions and Answers8 http://allinterviewsbooks.blogspot.com/2008/07/c-interview-questions-and-answers8.html C# Interview Questions and Answers7...
0
by: ramu | last post by:
C# Interview Questions and Answers8 http://allinterviewsbooks.blogspot.com/2008/07/c-interview-questions-and-answers8.html C# Interview Questions and Answers7...
0
by: reema | last post by:
EJB Interview Questions http://interviewdoor.com/technical/EJB-Interview-Questions.htm CSS Interview Questions http://interviewdoor.com/technical/CSS-Interview-Questions.htm C Interview Questions...
0
by: reema | last post by:
EJB Interview Questions http://interviewdoor.com/technical/EJB-Interview-Questions.htm CSS Interview Questions http://interviewdoor.com/technical/CSS-Interview-Questions.htm C Interview Questions...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.