• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 194
  • Last Modified:

How to keep an argument const over a large call stack?

Hi thr

My problem is that i have a large call stack. the argument passed by function at bottom of
stack is used and passed on to next function by all the functions in call stack. but i want
that this argument shouldnt be modified( it can only be used) by any of the functions.
so do i have to write const in arguments of each of my function(they r too many!!) ?
isnt there a better and simple way? (And i dont want to use a global flag).

thanks

Jastej.
0
jastiwana
Asked:
jastiwana
1 Solution
 
novitiateCommented:
Even if its taxing bit, but that's the right way to do it. It also tells some one looking at the prototype that the arguments is read only.

_novi_
0
 
grg99Commented:
Opinions vary, but to me if your call stack gets more than 6 levels deep, it often means you have too many levels of abstraction.  Abstraction is good, but can get in the way if overdone.

0
 
smpoojaryCommented:
Can you use template
-Mahesh
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
itsmeandnobodyelseCommented:
>>>> but i want that this argument shouldnt be modified

constness of arguments is only important for pointers and references. And even for these it is some kind of self-restriction as the machine code isn't different as long as none of the functions actually modifies a pointer or reference. So, it is highly recommended to use const arguments wherever possible, but not essential if none of the functions actually modifies the varable.

>>>> (they r too many!!)

It shouldn't be much difficult or time consuming to add a 'const' to some function arguments. If you have hundreds of functions you should overthink your design and IMHO constness of arguments is a minor problem then.

Regards, Alex
0
 
jastiwanaAuthor Commented:
Fine.. i agree with u guys that using const is the right solution. but lets make the problem more specific.
suppose i have two call stacks
a->b->c->d->e->f
z->b->c->d->e->f
Now thr r some arguments that are passed on from a to f, and z to f as well. two flows are
performing almost same task. but i want that if the flow is the 2nd one, then f should not
change its agruments, in case of 1st flow it can modify arguments. so is it possible for
f to know that whether the call has come from a or z.

thnx
0
 
itsmeandnobodyelseCommented:
>>>> so is it possible forf to know that whether the call has come from a or z.

No, beside you would like to store additional call stack information as a debugger does.

In C++ you could use polymorphism to control whether f(..) could/should modify arguments. That is done by providing two functions f having different argument lists:

   int f(char* psz, bool& modified);
   int f(const char* pszc);


In your sample it would need to provide two versions of b, c, d, e as well, though I am sure I would find a better solution if you would post some real code. In C++ both templates and virtual functions give a mighty means to gain different behaviour at runtime what is somewhat more intelligent than forcing an argument to be const sometimes at compile time.

Regards, Alex


0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now