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

Posted on 2005-05-01
Last Modified: 2010-04-01
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).


Question by:jastiwana
    LVL 8

    Expert Comment

    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.

    LVL 22

    Expert Comment

    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.

    LVL 4

    Expert Comment

    Can you use template
    LVL 39

    Expert Comment

    >>>> 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

    Author Comment

    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
    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.

    LVL 39

    Accepted Solution

    >>>> 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


    Featured Post

    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

    Join & Write a Comment

    Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
    Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
    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…
    The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

    728 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

    15 Experts available now in Live!

    Get 1:1 Help Now