Solved

subclass versus typedef/#define

Posted on 1998-06-29
7
261 Views
Last Modified: 2011-09-20
A long question,  but I think needs only a shorter answer:

I have a subclass with several constructurs.  One constructor is like this:
name::name(beginning text, data, ending text)
Lets say beginning text and ending texts are  "begin" and "end"  respectively
so frequently I don't want to define with:
name   variablename("begin", x,"end") but just with
name2   variablename(x) where x is the main data.

ok, now there is (at least) two way to do this:
1. make another subclass and initaialize the beginning and ending text to "begin", and "end".

2. yes? suggestions? one is I think with typedef or #define (preprocessor command)
My problem is I don't know how to type def such a thing, It should be someth like:
typedef name   name2("begin", here should x come somehow, "end");
so that you can define with
name2   variablename(x);
On top of this I'd like the definition:
name2  variablename;
to work to and result in :  
name("begin","end");

or is this just nonsens, any suggestion about the second way of doing what I want?
0
Comment
Question by:moonlight
  • 3
  • 3
7 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 1166848
You can't do it with typedef.  (Not that I can see at least.  I certainly never tried before)

You can do it with the preprocessor.  but don't.  you'll regret it.  I don't know when and I don't why, but I do know you'll regret it.  Avoid preprocessor like the plague it is.

I suspect there is a better way to get to wher you want to go.  What is the ultimate goal here?  Why are you doing this?
0
 
LVL 3

Author Comment

by:moonlight
ID: 1166849
it's for an assignment.
0
 
LVL 3

Author Comment

by:moonlight
ID: 1166850
I dont know how to do it with preprocessor either. show me how.
What I know you can have things like:
#define name2(x) name("begin", x, "end")
but then how do I declare variables?
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 22

Accepted Solution

by:
nietod earned 10 total points
ID: 1166851
You could do
#define name2(Prm)  ("begin",Prm,"end")

SomeType VariableName name2(5);

OR

#define name2(Var,Prm)  Var("begin",Prm,"end")

SomeType  name2(VariableName,5);

0
 
LVL 5

Expert Comment

by:yonat
ID: 1166852
You can use defualt arguments (but then you'll have to change the arguments' order). It looks like that:
    name(void* data, string begining = "begin", string ending = "end");
0
 
LVL 22

Expert Comment

by:nietod
ID: 1166853
Yonat, that sounds much better to me.  But I think there is something special about these parameters, like they are being used as semaphores of some kind.  So I think one has to be at the begining and one at the end.  Just a guess.  
0
 
LVL 3

Author Comment

by:moonlight
ID: 1166854
Thank you nietod, works fine.
And, I'm afraid I can't change the order, besides,
I need to use several of these start and end tags, which means
one default argument can't help..but I hadn't written that of course
ok, thank you both
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now