473,698 Members | 2,434 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

slightly interresting derrived class problem

I start off with an interface class that has no data members and a
handful of virtual functions.
First Question: is that allowed ?
I then derived from this class and it gets included into a couple of
other classes that need that interface. The derived class had a number
of data members including a struct that allow it to do its dirty work.

When I directly access any data members (through pointers or through
public data members) in the derrived class hilarity ensues. I found
that the value returned is offset by 4 bytes( 32bits ).

for example, if I wanted:
class1.public_s truct.int0
I would get
class1.public_s truct.int1
more curiously this problem would only show up when i ran the release
version of the code. The debug version behaved correctly.

After an entire wasted morning I added an unused data member to the
very first interface class and all problems dissapeared like a fart in
the wind.

Main Question: Is this a compiler error or did I violate the standard?

May 29 '06 #1
9 1641
easy wrote:
I start off with an interface class that has no data members and a
handful of virtual functions.
First Question: is that allowed ?
Sure, why not? A class can (I think) be empty, or it can contain a
number of variables and/or functions (including virtual ones).
I then derived from this class and it gets included into a couple of
other classes that need that interface. The derived class had a number
of data members including a struct that allow it to do its dirty work.

When I directly access any data members (through pointers or through
public data members) in the derrived class hilarity ensues. I found
that the value returned is offset by 4 bytes( 32bits ).
First of all, see if you can live without public members. Some get/set
functions might be a lot cleaner.

And it'd be great if you could describe what exactly explodes in your
code when you access a public member. If you use the GNU compiler, you
could even try to compile it with optimization AND debug (-g -O2 for
instance), to see if the bug appears.
for example, if I wanted:
class1.public_s truct.int0
I would get
class1.public_s truct.int1


Is class1 the derived class, or are you doing something with the base class?
May 29 '06 #2
easy wrote:
I start off with an interface class that has no data members and a
handful of virtual functions.

First Question: is that allowed ?
If you do it correctly, yes.
I then derived from this class and it gets included into a couple of
other classes that need that interface. The derived class had a number
of data members including a struct that allow it to do its dirty work.
Whoo hoo! Dirty work? Ok.
When I directly access any data members (through pointers or through
public data members) in the derrived class hilarity ensues. I found
that the value returned is offset by 4 bytes( 32bits ).

for example, if I wanted:
class1.public_s truct.int0
I would get
class1.public_s truct.int1

more curiously this problem would only show up when i ran the release
version of the code. The debug version behaved correctly.

After an entire wasted morning I added an unused data member to the
very first interface class and all problems dissapeared like a fart in
the wind.

Main Question: Is this a compiler error or did I violate the standard?


It sounds like you've got some kind of data slicing going on. Some
place in there, your pointer thinks it's pointing at a different kind
of
object from what it is actually pointing at. The result is, when it
tries to find int0 it is looking where it expects int0, but it has
found
instead int1. (Or the other way over.)

In debug mode, there may be other stuff in there that shoves stuff
around by amounts that are not defined in the standard. It could
quite easily be that the base class is shoved forward 4 bytes, and
the derived by 8. And so when you make your off-by-4 errror, it
gets another error in the other direction from the debug info.

Post a minimal compilable code sample that shows the problem,
and we may be able to help.
Socks

May 29 '06 #3
easy wrote:
I start off with an interface class that has no data members and a
handful of virtual functions.
First Question: is that allowed ?
Uh... Yes. Does your compiler tell you something you don't believe
it should?
I then derived from this class and it gets included into a couple of
other classes that need that interface. The derived class had a
number of data members including a struct that allow it to do its
dirty work.
OK...
When I directly access any data members (through pointers or through
public data members) in the derrived class hilarity ensues. I found
that the value returned is offset by 4 bytes( 32bits ).

for example, if I wanted:
class1.public_s truct.int0
I would get
class1.public_s truct.int1
That's bizarre.
more curiously this problem would only show up when i ran the release
version of the code. The debug version behaved correctly.
That sounds like an error in the compiler...
After an entire wasted morning I added an unused data member to the
very first interface class and all problems dissapeared like a fart in
the wind.

Main Question: Is this a compiler error or did I violate the standard?


To be able to tell you for sure, we'd need to see the code.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 29 '06 #4
Ulrich Hobelmann wrote:
easy wrote:
I start off with an interface class that has no data members and a
handful of virtual functions.

First Question: is that allowed ?
Sure, why not? A class can (I think) be empty, or it can contain a
number of variables and/or functions (including virtual ones).


Definitely. It's a very common idiom. However, some gurus (read:
Sutter) recommend preferring to avoid public virtual functions. See
_Exceptional C++ Style_ for full exposition, but basically the
reasoning is that with public virtual functions, you're combining two
interfaces into one, which is an unnecessary form of coupling and hence
needlessly restrictive. The first interface is the base class's public
interface; the second interface (in a slightly different sense of the
word) is the "customizat ion interface" presented to derived classes.
Two different audiences.

What he recommends instead is to use simple forwarding in trivial
cases, and to make use of idioms such as the Template Method pattern in
more complex cases. For example:

class Base
{
public:
void doStuff() { reallyDoStuff() ; }
protected:
virtual void reallyDoStuff() ;
};

It may seem at first that you'd rarely get beyond such trivial cases
and really wind up using Template Method a lot, but that's a result of
being stuck in the mindset of combining these two interfaces. A member
function corresponds to a responsibility -- surely it happens quite
frequently that there is not a 1:1 mapping between publicly visible
responsibilitie s and behavior intended to be customized by subclasses!
First of all, see if you can live without public members. Some get/set
functions might be a lot cleaner.


I agree on the public members -- except in behaviorless structs,
they're generally to be avoided. However, I disagree with simply
replacing your "int x" public data with "int getX()" and "void
setX(int)." There are some arguments as to how this is better than
simple public data, but whatever advantages exist simply aren't enough.
The real answer is design -- put the data where it wants to be. Don't
provide data, provide computation. I'd go into this more, but you can
google something like "getters setters evil" for more.

Luke

May 30 '06 #5
Would it be better if I posted some files to some webspace or copied
the contents to the forum? Either way I'll post tomorrow when I get
back to work.

May 30 '06 #6
"First of all, see if you can live without public members. Some
get/set functions might be a lot cleaner."

The public members were only used by me to try and figure out what the
hell was going on.

May 30 '06 #7
* easy:
I start off with an interface class that has no data members and a
handful of virtual functions.

First Question: is that allowed ?
Yes.

I then derived from this class and it gets included into a couple of
other classes that need that interface. The derived class had a number
of data members including a struct that allow it to do its dirty work.

When I directly access any data members (through pointers or through
public data members) in the derrived class hilarity ensues. I found
that the value returned is offset by 4 bytes( 32bits ).

for example, if I wanted:
class1.public_s truct.int0
I would get
class1.public_s truct.int1
more curiously this problem would only show up when i ran the release
version of the code. The debug version behaved correctly.
Post a minimal example that compiles.

For other requirements on posting example code, see the FAQ item "How do
I post a question about code that doesn't work correctly?", currently at
<url: http://www.parashift.c om/c++-faq-lite/how-to-post.html#faq-5.8>.

After an entire wasted morning I added an unused data member to the
very first interface class and all problems dissapeared like a fart in
the wind.

Main Question: Is this a compiler error or did I violate the standard?


Probably the latter, but not in the way you indicated above.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
May 30 '06 #8
easy wrote:
Would it be better if I posted some files to some webspace or copied
the contents to the forum? Either way I'll post tomorrow when I get
back to work.


Please quote the post you're replying to, as a matter of longstanding
Usenet conventional courtesy.

You should post your source code directly to the newsgroup -- however,
it's important to post a *minimal* example that demonstrates the
problem. Most problems appropriate for newsgroup discussion can be
described in a page or so of code. *Compilable* code, mind.

Luke

May 30 '06 #9
Luke Meyers wrote:
Definitely. It's a very common idiom. However, some gurus (read:
Sutter) recommend preferring to avoid public virtual functions. See
_Exceptional C++ Style_ for full exposition, but basically the
reasoning is that with public virtual functions, you're combining two
interfaces into one, which is an unnecessary form of coupling and hence
needlessly restrictive. The first interface is the base class's public
interface; the second interface (in a slightly different sense of the
word) is the "customizat ion interface" presented to derived classes.
Two different audiences.
Hm, interesting view. Coming from a Java background, I'd rather say
that the whole point of virtual functions is being public. A set of
virtual functions is the base class's interface that other classes can
implement. So, I don't think they are harmful (even if usually I
strongly prefer non-virtual functions and composition when possible,
both for performance and for clarity).

I'd be careful with (non-virtual) inheritance (i.e. with not
implementing a clear, documented interface).
I agree on the public members -- except in behaviorless structs,
they're generally to be avoided. However, I disagree with simply
replacing your "int x" public data with "int getX()" and "void
setX(int)." There are some arguments as to how this is better than
simple public data, but whatever advantages exist simply aren't enough.
The real answer is design -- put the data where it wants to be. Don't
provide data, provide computation. I'd go into this more, but you can
google something like "getters setters evil" for more.


Cool, thanks for the link; I'll look into it. Myself, I sometimes avoid
get/setters (and use structs instead), just for convenience, but I
thought I should mention the "official" view ;)
May 30 '06 #10

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

Similar topics

9
2123
by: Ingo Nolden | last post by:
Hi there, I am writing c++ for some months now. I think I know the language now, but not yet all the tricky things of stl. For a kernel which is not using mfc I am writing a serialization. For some reasons I want to implement it on my own. One goal is to make it robust against version changes and corrupted files. That means, when an object cannot be deserialized, it should be skipped. The validity of the remaining document can be verified...
4
2234
by: Deniz Bahar | last post by:
Hello, A couple days ago my friend (OOP guy) shows me what OOP was all about in C++. This morning I figured I can do pretty much the same thing with C (by putting function pointers in structures and using Macros). I've gone pretty far but I'm running into problems because the function pointers within the structures need an extra argument (a pointer to that type of structure) so they know which data to operate on. Also, I have to...
3
420
by: jrhoads23 | last post by:
If you look at a standard Button in a .NET Windows Forms app, you will notice its default BackColor is "Control" and it has a 3D raised border which is 2 pixels wide. The outer edge on the left and top is "ControlLightLight" color and the inner edge on the left and top is "ControlLight". The outer edge on the right and bottom is "ControlDarkDark" and the inner edge on the right and bottom is "ControlDark". All of these colors are defined...
2
2629
by: Elephant | last post by:
Hello, question, I want to make a COM-compatible .NET DLL (Visual Basic). For this I need an interface. The DLL has a class that must contain methods that can be used without making an instance of the class first. So I have a class that has methods that derrive from an interface, and the same class has methods that are Shared....but methods that are derrived from an interface are not allowed to be Shared...
1
1405
by: Anonymous | last post by:
I am working on a suite of existing libraries and I anticipate a future problem. the problem (as i see it), arises as a result of the following: There is a class A that resides in librray A, and class B that resides in library B. class B is declared as ff: class B {
1
2491
by: Phil Latio | last post by:
I have a number of spreadsheets, each with between 1000-6000 rows (each row is a property) and they all need to be combined into a single database. Each spreadsheet contains slightly different information and 80% of the properties in spreadsheet_01 also appear spreadsheet_02. Spreadsheet_03 contains slightly different information again and perhaps has 60% of properties in this one also appear in other spreadsheets. The problem is not...
3
1964
by: pauldepstein | last post by:
I am using the latest version of Visual c++ on windows XP. I am trying to build a project and get a lnk1104 error regarding a file of the form .../../ ProjectName.lib No other errors, just this one. I've googled this error but not found anything useful at my newbie level. I understand the issue may be one of including the right files. I therefore went to Additional Include Directories on the Property Pages but I would need help...
4
1679
by: Ken Fine | last post by:
I know about this: http://silverlight.net/forums/14.aspx and this: http://forums.asp.net/1127.aspx Silverlight Beta 2 was announced at TechEd. I was impressed. When does Silverlight get a first-class newsgroup? -KF
4
1355
by: rlwebbnafex | last post by:
Hello, I have never used generic programming so I need a little help for an idea on what I can do. I have defined a lot of classes like the following. The classes are all pretty much the same, they just represent data structures that I need to use later. I create an vector of each of these data structures later. It is just a record-keeping system. Anyhow, is there a way to create a generic structure and define the few member functions I...
0
8604
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8897
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8862
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7729
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
6521
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
5860
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
4370
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...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3050
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

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.