473,721 Members | 2,259 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 3610
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
5137
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
1461
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 compiler error on the call to add_varargs_method("get2",&Derived2::get2, "get2()\n"); in...
4
51178
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 other suggestions are more than welcome. (BTW this is not for one constant, I have multiple...
45
6351
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 { // Random base class. public class A { protected int i;
11
3139
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 current entries for that page, and adds a new entry with some information regarding the current...
3
2709
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 method in the Global.asax file to create an identity and principal which are attached to the Context...
30
6082
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
5341
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 resource required
3
1479
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
8840
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
8730
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,...
0
9215
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...
1
6669
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
5981
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
4484
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
4753
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2576
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2130
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.