473,756 Members | 2,996 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Don't want to define derived class?

The rule of inheritance states that you define from top to bottom.
Sometimes, you want to define base class and set reference from
dervied class to base class, but you violate the rule.
Here is an example of my code below.

class A {};
class B : public A {};

int main(void)
{
// A a;
B &b = a; // Compiler cannot compile -- error
// B &b = reinterpret_cas t<B&( a ); // OK

B b;
A &a = b; // OK

return 0;
}

Please state your opinion. Is it ok to violate the rule? is this
code portable to all different machines?

Nephi
Sep 2 '08 #1
3 1480
Immortal Nephi wrote:
The rule of inheritance states that you define from top to bottom.
Sometimes, you want to define base class and set reference from
dervied class to base class, but you violate the rule.
Here is an example of my code below.

class A {};
class B : public A {};

int main(void)
{
// A a;
B &b = a; // Compiler cannot compile -- error
// B &b = reinterpret_cas t<B&( a ); // OK

B b;
A &a = b; // OK

return 0;
}

Please state your opinion.
You are lying to the compiler and asking for trouble.
Is it ok to violate the rule?
No.
is this code portable to all different machines?
Undefined behaviour is never portable.

--
Ian Collins.
Sep 2 '08 #2
On 2008-09-03 00:35, Immortal Nephi wrote:
The rule of inheritance states that you define from top to bottom.
Sometimes, you want to define base class and set reference from
dervied class to base class, but you violate the rule.
Here is an example of my code below.

class A {};
class B : public A {};

int main(void)
{
// A a;
B &b = a; // Compiler cannot compile -- error
// B &b = reinterpret_cas t<B&( a ); // OK

B b;
A &a = b; // OK

return 0;
}

Please state your opinion. Is it ok to violate the rule? is this
code portable to all different machines?
Absolutely not, consider the following code:

class Base
{
public:
virtual ~Base() {}
};

class Derived
{
int i;
public:
void foo() { i = 1; }
};

int main()
{
Base a;
Derived& b = reinterpret_cas t<Derived&>(a) ;

b.foo();
}

When I run foo(), exactly what memory is it that I set to 1? When I run
this in VS2008 I overwrite the vtable pointer but.

You can never cast a class of base type to a class to derived type since
there does not exist a is-a relation in that direction (i.e. which a
derived class is a base class, a base class is not a vehicle. That would
be the same thing as saying that a vehicle is a car just because a car
is a vehicle).

--
Erik Wikström
Sep 3 '08 #3
On Sep 3, 11:06*am, Erik Wikström <Erik-wikst...@telia. comwrote:
On 2008-09-03 00:35, Immortal Nephi wrote:


* *The rule of inheritance states that you define from top to bottom.
Sometimes, you want to define base class and set reference from
dervied class to base class, but you violate the rule.
* *Here is an example of my code below.
class A {};
class B : public A {};
int main(void)
{
// A a;
* *B &b = a; // Compiler cannot compile -- error
// B &b = reinterpret_cas t<B&( a ); // OK
* *B b;
* *A &a = b; // OK
* *return 0;
}
* *Please state your opinion. *Is it ok to violate the rule? *is this
code portable to all different machines?

Absolutely not, consider the following code:

class Base
{
public:
* virtual ~Base() {}

};

class Derived
{
* int i;
public:
* void foo() { i = 1; }

};

int main()
{
* Base a;
* Derived& b = reinterpret_cas t<Derived&>(a) ;

* b.foo();

}

When I run foo(), exactly what memory is it that I set to 1? When I run
this in VS2008 I overwrite the vtable pointer but.
You are correct that vtable pointer is overwritten. For example,
you place member variables in base class. You set reference to
derivd1 class and derived2 class. Then, you try to modify member
variable in derived1 class "AND" derived2 class which they are
inherited from base class. It does not have any problem. Sharing
member variables from base class to both derived classes are fine.
According to my test, both derived1 and derived2 classes can use
member variables from base class without any problem. If you try to
use member variables inside derived1 class and derived2 class, then
yes, you can notice. Attempt to modify derived1's member variables
overwrites derived2's member variable. Member variables in base class
are unaffected.
Friend keyword is the only option if they want to share member
variables in both two base classes. Note--static member variables are
not the option if you want to use friend keyword.

Nephi
You can never cast a class of base type to a class to derived type since
there does not exist a is-a relation in that direction (i.e. which a
derived class is a base class, a base class is not a vehicle. That would
be the same thing as saying that a vehicle is a car just because a car
is a vehicle).

--
Erik Wikström- Hide quoted text -

- Show quoted text -
Sep 3 '08 #4

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

Similar topics

4
1842
by: Nataraj M | last post by:
Hi, I just don't want anybody derive a class from my class. For example: /////////////////////// //MY CODE class MyClass { .... }; ///////////////////////
8
2579
by: Pete C | last post by:
In this section of the FAQ: http://www.parashift.com/c++-faq-lite/pointers-to-members.html#faq-33.10 an abstract destructor is given a definition with the cryptic comment "It's faster this way, trust me". Why? Is it faster to type? I notice that the derived classes in that example seem mysteriously not to require a destructor.
3
1436
by: Francois | last post by:
Hi, I am trying to extend a .NET class for overriding some methods. The class that I try to override is : System.Web.UI.WebControls.CheckBox. Its full definition is : public class CheckBox : WebControl, IPostBackDataHandler (notice the interface that is implemented) I want to override 3 methods belonging to that class:
18
1452
by: **Developer** | last post by:
I always define events with the parameters ByVal sender As Object, ByVal e As EventArgs Even if they are not used. Seems I read someplace that's the thing to do. So I then do:
5
1965
by: Rob | last post by:
In many articles related to VB.net the word "class" is used... How many meanings are there to this word ? "possible to derived a class from another" "forms are full-fledged classes" "base class"
31
1919
by: Jo | last post by:
class A { public: char text_a; A() { *text_a=0; } ~A() {} }; //-----------------------------------------------------------------------------
11
1435
by: Sachin Garg | last post by:
I need to build two executables from my code, one having all the code (and thus application features) and other not having it all. How to best manage the code that shouldn't go in one of the executables? My first thought is to use conditional compilation with #define and #ifdef etc but its getting messy, are there better ways to manage this? The problem in detail: #. There is a class A with virtual foo1 and foo2
2
1678
by: Anton Bredikhin | last post by:
Hello! I have a class hierarchy that consists mainly of one base class CtxBase, and two children: LoginCtx and DisconnectCtx. Now, there's a need to provide an interfaces for this hierarchy. I'd like to export this classes from a DLL. And I'm in doubt how can I achieve this? My first option was to define a base interface (ICtxBase) and then
1
6712
by: Stuart Brockman | last post by:
Hi, I don't quite get what is going on in this code example: --------------------------- #include <iostream> using namespace std; class Base{ public:
0
9431
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10014
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9844
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8688
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7226
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6514
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5119
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3780
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3326
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.