473,574 Members | 3,159 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

global typedefs and #define /\ global inheritance base

Hi folks,

I hope this is not "off topic"! :-)

Consider the next code:

/* Declarations of types that could become platform-dependent */

#define MyChar char
#define MyInt int
#define MyUnsLong unsigned long
#define MyShort short
#define MyCharPtr MyChar *
#define MyBoolean int

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

#ifdef DEBUG_MODE
inline void* operator new(size_t size, char* aFile, int aLine);
inline void* operator new[](size_t size, char* aFile, int aLine);
#else
inline void* operator new(size_t size);
inline void* operator new[](size_t size);
#endif
inline void operator delete(void* object);
inline void operator delete[](void* object);

};

Most of you know java quit good, so there is "java.lang.Obje ct" and
classes are inheriant to this one.
So when and how would this become interessting for my c++-design? Why
should I design my classes in that way?
Another thing are the #define (for cross-plattform-stuff).
Code becomes worse dealing with these MyChar's but could it be
necessary to build cross-platform code in that way?

Any advice in dealing with such designes?
Today I'm a win-developer(scien tific education stuff), so there is not
so much cross-plattform knowledge.

Dec 5 '05 #1
5 3600
eiji wrote:
I hope this is not "off topic"! :-)

Consider the next code:

/* Declarations of types that could become platform-dependent */

#define MyChar char
#define MyInt int
#define MyUnsLong unsigned long
#define MyShort short
#define MyCharPtr MyChar *
#define MyBoolean int

class Base
{
public:
inline Base() {};
inline ~Base() {};
Drop the semicolons after }s and the words "inline". They are totally
superfluous, and make code harder to read.

#ifdef DEBUG_MODE
inline void* operator new(size_t size, char* aFile, int aLine);
inline void* operator new[](size_t size, char* aFile, int aLine);
#else
inline void* operator new(size_t size);
inline void* operator new[](size_t size);
#endif
inline void operator delete(void* object);
inline void operator delete[](void* object);

};

Most of you know java quit good
Yep. It quit good, quit all over the place... Oh, did you mean "quite
well"?
, so there is "java.lang.Obje ct" and
classes are inheriant to this one.
You mean, all non-built-in types derive from 'java.lang.Obje ct' class?
OK. Yes, they do. And all functions are virtual.
So when and how would this become interessting for my c++-design?
Is that a question you want _us_ to answer? It's _your_ design. How
should *we* know anything about it?
Why
should I design my classes in that way?
I don't think you should, actually. Are you trying to figure out how to
use Java's approaches to modeling worlds in C++? It's generally A BAD
IDEA(tm). Java has its advantages, C++ has its own. Applying designs
from one to the other is not always beneficial.
Another thing are the #define (for cross-plattform-stuff).
Code becomes worse dealing with these MyChar's but could it be
necessary to build cross-platform code in that way?
Yes, it could, I suppose.
Any advice in dealing with such designes?
Put them in a separate file. How else could you "deal" with them?
Today I'm a win-developer(scien tific education stuff), so there is not
so much cross-plattform knowledge.


Do you really need it? Are you planning on moving on to some "greener
pastures"? Cross-platform development is a bitch, you know.

V
Dec 5 '05 #2
The "Base"-class is just a guess. (not implemented that way)

I often see c++ design where everything is derived from one base, but I
could not find out when there is a real benefit or even where are
possible disadvantages(s peed?, space?).

So the question would be:
Is there known use case for that? ( And I mean where everything is
derived from "Base")

Dec 5 '05 #3
eiji wrote:
Hi folks,

I hope this is not "off topic"! :-)
It is not really, but it is hard to answer because it is not specific.
Consider the next code:

/* Declarations of types that could become platform-dependent */

#define MyChar char
#define MyInt int
#define MyUnsLong unsigned long
#define MyShort short
#define MyCharPtr MyChar *
#define MyBoolean int
I would use typedefs here, especially if this is in a header, and I
would put it in a namespace. #defines are best avoided.
class Base
{
public:
inline Base() {};
inline ~Base() {};
These are already inline by defining them in the class and the
semicolumns are superflous.
#ifdef DEBUG_MODE
inline void* operator new(size_t size, char* aFile, int aLine);
inline void* operator new[](size_t size, char* aFile, int aLine);
#else
inline void* operator new(size_t size);
inline void* operator new[](size_t size);
#endif
inline void operator delete(void* object);
inline void operator delete[](void* object);

};

Most of you know java quit good, so there is "java.lang.Obje ct" and
classes are inheriant to this one.
So when and how would this become interessting for my c++-design?
Only when it is necessary. The Object class is a worst-case of
inheritance, when all the classes share a common base class. Usually,
you will want to have seperate hierarchies in your program.

The problem with a universal base class (UBC) is that it prevents
static type-safety, much as void pointers do in C++. It also makes no
sense in many circumstances. Most of the time, you will work with more
specific base classes (such as Fruit, Vehicle or Animal).

It also brings the problem of "what should I put in a UBC"? What
functionnalitie s are universal to all your classes in your program? A
ToString() member function? Are you sure all the classes must have a
conversion to string? Serialization? Clone? Does it makes sense to call
Wait() to pause a thread on a Banana object?

It is much better to provide exactly what you need, instead of
providing too much.
Why
should I design my classes in that way?
If some classes share a common subset and you want to access them by
that subset (a banana is a fruit), derive them from a class containing
only that subset. If you can find a common subset for all the classes
in your program and you want to access them by that subset, derive them
all from a UBC.
Another thing are the #define (for cross-plattform-stuff).
Code becomes worse dealing with these MyChar's but could it be
necessary to build cross-platform code in that way?


There will be sometimes when your code will have to be ported, but you
may never have to. A good balance between genericity and specificity is
vital.
Jonathan

Dec 5 '05 #4
eiji wrote:
The "Base"-class is just a guess. (not implemented that way)

I often see c++ design where everything is derived from one base, but I
could not find out when there is a real benefit or even where are
possible disadvantages(s peed?, space?).
The disadvantage is confusion that such "model" creates. When you model
your "world" where everything is derived from the same class, what kind
of functionality do you give that class? A virtual destructor, and that's
about it? Base classes are only needed when there is some commonality
between _ALL_ the types deriving from it. What kind of commonality do you
see between _literally_ all elements of your design? I don't see any.
Not even the need in polymorphic deletion. So, none of my designs have
the common base class.
So the question would be:
Is there known use case for that? ( And I mean where everything is
derived from "Base")


Java. Beyond that, I don't know of any. But wait, didn't you just say
that you "often see c++ design where everything is derived from one base"?
So, there must be a known use case for that, right? I mean, since you
"often see" it...

V
Dec 6 '05 #5
Victor Bazarov wrote:
eiji wrote:
I hope this is not "off topic"! :-)

Consider the next code:

/* Declarations of types that could become platform-dependent */

#define MyChar char
#define MyInt int
#define MyUnsLong unsigned long
#define MyShort short
#define MyCharPtr MyChar *
#define MyBoolean int
To the OP: use typedef instead of #define.

typedef char MyChar;
//...
class Base
{
public:
inline Base() {};
inline ~Base() {};
Drop the semicolons after }s and the words "inline". They are totally
superfluous, and make code harder to read.


Actaully, I would drop the whole thing. In this case, the compiler will just
create the same thing automatically.
#ifdef DEBUG_MODE
inline void* operator new(size_t size, char* aFile, int aLine);
inline void* operator new[](size_t size, char* aFile, int aLine);
#else
inline void* operator new(size_t size);
inline void* operator new[](size_t size);
#endif
inline void operator delete(void* object);
inline void operator delete[](void* object);

};

Most of you know java quit good


Yep. It quit good, quit all over the place... Oh, did you mean "quite
well"?


Don't make fun of people just because their english is not as good as yours,
even if that gives you a feeling of superiority.
, so there is "java.lang.Obje ct" and
classes are inheriant to this one.


You mean, all non-built-in types derive from 'java.lang.Obje ct' class?
OK. Yes, they do. And all functions are virtual.
So when and how would this become interessting for my c++-design?


Is that a question you want _us_ to answer? It's _your_ design. How
should *we* know anything about it?
> Why
should I design my classes in that way?


I don't think you should, actually. Are you trying to figure out how to
use Java's approaches to modeling worlds in C++? It's generally A BAD
IDEA(tm). Java has its advantages, C++ has its own. Applying designs
from one to the other is not always beneficial.

Another thing are the #define (for cross-plattform-stuff).
Code becomes worse dealing with these MyChar's but could it be
necessary to build cross-platform code in that way?


Yes, it could, I suppose.
Any advice in dealing with such designes?


Put them in a separate file. How else could you "deal" with them?
Today I'm a win-developer(scien tific education stuff), so there is not
so much cross-plattform knowledge.


Do you really need it? Are you planning on moving on to some "greener
pastures"? Cross-platform development is a bitch, you know.


It's actually a lot easier if you consider it from the beginning. Making an
large program that was not written with portability in mind portable is "a
bitch", as you say it.

Dec 6 '05 #6

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

Similar topics

88
5094
by: Tim Tyler | last post by:
PHP puts most of its functions into a big flat global namespace. That leads to short function names - but creates a namespace minefield for programmers. Lots of the functions are legacies from the days before PHP got object-oriented features. For instance we currently have:
0
1453
by: John Hunter | last post by:
I am using pycxx 5.2.2 to implement some extension code and have a problem relating to inheritance. I have a pure virtual base class and two concrete derived classes. In the code below, everthing works fine in that the derived classes can use the base method get, but if I try and add a method only available to Derived2, eg, get2, I get a...
4
51167
by: Amadelle | last post by:
Hi all and thanks again in advance, What is the best way of defining global constants in a C# application? (A windows application with no windows forms - basically a set of classes). Would it be a wise idea to create a clsCommonApp and let all other classes to be derived from that class? and define all constants in that base class? Any...
45
6333
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using parameters, I get CS1501 (no method with X arguments). Here's a simplified example which mimics the circumstances: namespace InheritError { //...
11
3113
by: Wolfgang Kaml | last post by:
I am not sure if this is more of an expert question, but I am sure that they are out there. I'd like to setup a general application or bin directory on my Win2003.Net Server that will hold some useful utils that more pages on that server can use. As an example, I have created a Page Counter class that opens an Access .mdb file, counts the...
3
2705
by: Joe Reazor | last post by:
I understand how Web.Config inheritance works between a parent application and sub applications under the parent. But what I was wondering was if there was a similar way to do the same thing for the Global.asax class? Reason being, I am setting up user authentication and authorization. I have coded up my Application_AuthenticateRequest...
30
6063
by: junky_fellow | last post by:
I was looking at the source code of linux or open BSD. What I found that lots of typedefs were used. For example, consider the offset in a file. It was declared as off_t offset; and off_t is typedefed as typedef long off_t; I wanted to know what advantage do we get by typedefs ? Why we did not declare
1
5333
by: David Lozzi | last post by:
Hello, Writing a web app in asp.net using vb. Just created a new page, added a few minor controls and now blamo, the below error: HELP Server Error in '/newsite' Application. -------------------------------------------------------------------------------- Compilation Error Description: An error occurred during the compilation of a...
3
1469
by: Immortal Nephi | last post by:
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) {
0
7841
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...
0
8101
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. ...
0
8271
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...
1
7858
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...
0
8137
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...
1
5654
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...
0
5335
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...
0
3793
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2273
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.