473,385 Members | 2,005 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,385 software developers and data experts.

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.Object" 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(scientific education stuff), so there is not
so much cross-plattform knowledge.

Dec 5 '05 #1
5 3592
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.Object" and
classes are inheriant to this one.
You mean, all non-built-in types derive from 'java.lang.Object' 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(scientific 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(speed?, 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.Object" 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
functionnalities 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(speed?, 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.Object" and
classes are inheriant to this one.


You mean, all non-built-in types derive from 'java.lang.Object' 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(scientific 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
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...
0
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...
4
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...
45
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...
11
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...
3
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...
30
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...
1
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....
3
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...

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.