• Advertisement
Sign in to follow this  

Accept class function parameters as class variables

This topic is 1458 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I know this is a simple question, but one I've always been looking for a solution for.

 

class MAP

{

      Scene scene;

      void AddedToScene(Scene);

};

 

 

void SCENE::addedToScene(Scene newScene)
    {
        scene = newScene;
    }

 

 I know it's minor, but I don't like always having to put a scene = newscene statement in the function definition.

 

Is there any way to make the class function automatically accept a function parameter of the same name as a class variable.

 

void addedToScene(Scene scene)
    {

         // scene is automatically accepted as the class variable with the same name

    }

Share this post


Link to post
Share on other sites
Advertisement
Not in C++.

There is little point to do so anyway. Either the assignment is trivial (in this case it's probably better to have a public member and no accessor at all) or it's not trivial (because something else needs to be done aside from the pure assignment; in this case code would not be automatic anyway).

Share this post


Link to post
Share on other sites
It would be really hard for a compiler to tell which parameter maps to which member variable (i.e. you have 5 internal ints). Strictly speaking, the amount of time you spend on assigning to member variables is insignificant to the amount of time you should be spending on other things, so it shouldn't really matter in the end.

Share this post


Link to post
Share on other sites
This is just a bug waiting to happen. Consider the following example:

struct Foo
{
    int UberImportantData;


    int DoComputation(int UberImportantData)
    {
        // Well, shit. There goes my data.
    }
};
The way it currently works in C++ is nicer, because if I do have two identical identifiers that point to different variables, I can disambiguate freely using this-> or namespace resolution constructs (whichever is applicable).

Share this post


Link to post
Share on other sites

Why do you have a member variable that needs to have this assigned to it after each call to addedToScene anyway? Is this supposed to be tracking the last added value or something?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement