Jump to content

  • Log In with Google      Sign In   
  • Create Account


Accept class function parameters as class variables


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 CatmanFS   Members   -  Reputation: 187

Like
0Likes
Like

Posted 21 January 2014 - 09:04 AM

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

    }



Sponsor:

#2 BitMaster   Crossbones+   -  Reputation: 3696

Like
3Likes
Like

Posted 21 January 2014 - 09:08 AM

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

#3 incertia   Crossbones+   -  Reputation: 777

Like
0Likes
Like

Posted 21 January 2014 - 09:39 AM

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

#4 tonemgub   Members   -  Reputation: 647

Like
0Likes
Like

Posted 22 January 2014 - 02:42 AM


void SCENE::addedToScene(Scene newScene)

I think this should be:

void MAP::addedToScene(Scene newScene)



#5 ApochPiQ   Moderators   -  Reputation: 14430

Like
2Likes
Like

Posted 22 January 2014 - 09:12 PM

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

#6 Aardvajk   Crossbones+   -  Reputation: 5300

Like
1Likes
Like

Posted 23 January 2014 - 06:46 AM

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?






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS