Thread: How to easier use a Class?

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    162

    Question How to easier use a Class?

    Hi, all

    I was just wondering if it is possible to somehow skip writing the same class name over and over again, when calling or using its members. The following code might give a better understanding of my question.
    Code:
    Class ClassName // ClassName is declared as a class that goes by the name “Class”, very original ;)
    
    // The regular way of using a class:
    ClassName.FuncMember(); // calling a function member
    ClassName.VarMember = 1; // assign a value to a variable member
    
    // A easier way of using a class?:
    using ClassName; // I know this wont work, that’s the reason for this post
    
    .FuncMember(); // calling a function member
    .VarMember = 1; // assign a value to a variable member
    As you can see, I am looking for a keyword what will eliminate the painful repetition of class and structure names when using their members. Like in Visual Basic, it has the keyword “with” that simplifies the use of classes and such, but of course VB simplifies a lot of things (which isn’t necessarily a good thing).
    We haven't inherited Earth from our parents; instead we have borrowed her from our children - old Indian saying.

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    You can use the preprocessor directive #define to shorten what you have to type:

    #define Aa Classname

    That causes a straight substitution: anywhere in your file that the preprocessor encounters the letters Aa--except for in a comment, or as part of a character string between quotes--it will substitute Classname. So, you could do this:

    Aa.FuncMember();

    But, be careful--if you have another variable called MAap, the substitution will also occur there causing an error when the declaration for the variable MClassnamep cannot be found.

    Or, some text editors have variable name completion.

    Or, use copy and paste to put the class name in the clipboard, and then right click and paste it everytime you need it--no typing at all.
    Last edited by 7stud; 08-14-2003 at 03:43 AM.

  3. #3
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    I was just wondering if it is possible to somehow skip writing the same class name over and over again, when calling or using its members.
    Don´t think that is possible.
    01000111011011110110111101100100 011101000110100001101001011011100110011101110011 01100100011011110110111001110100 01100011011011110110110101100101 01100101011000010111100101110011 0110100101101110 01101100011010010110011001100101
    Good things don´t come easy in life!!!

  4. #4
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    But if you use the define keyword as 7stud illustrated, this 'hack' (could you call it that?) only works with one variable. If you define 2 or more objects you have to have multiple define-directives. Actually one define-directive is sufficient but every time you use a different 'variable' you have to redefine your define-directive. Also this solution isn´t type-safe.

    But my advice is to NOT skip the class name every time you use the class memebers.
    01000111011011110110111101100100 011101000110100001101001011011100110011101110011 01100100011011110110111001110100 01100011011011110110110101100101 01100101011000010111100101110011 0110100101101110 01101100011010010110011001100101
    Good things don´t come easy in life!!!

  5. #5
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    It is possible to make a macro that takes an index (for example), and appends that, so for example, Aa(7) would translate to ClassName7 (for many variables), but really, it isn't worth it. Just use copy-n-paste.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  6. #6
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    I always thought the object name made the code more informative but hey, to each his own right?
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  7. #7
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Originally posted by 7stud

    But, be careful--if you have another variable called MAap, the substitution will also occur there causing an error when the declaration for the variable MClassnamep cannot be found.
    No, it won't. Macro substitution isn't just a "search and replace", it selectively replaces certain types of tokens. In "MAap", the whole name is one token; macro substitution can't happen within one token -- try it out. Macros are replaced only if the entire token matches the macro name, and only certain types of tokens are even checked.

    As to the general question, you rarely use Classname::Method(), you usually use objectName.Method(). In this case, the object name is critical, you're directing which instance of the class you want to work with. If it was possible to remove it, you'd obfuscate your code beyond repair.

    In the case of a non-static function, it's never a good idea to use any kind of shorthand, even if any were possible. For a class filled with nothing but static functions (where you often call ClassName::Method(), etc.) put the methods in a namespace instead.

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    I was just thinking of the very same thing the other day.
    I thought an extension of the 'this' pointer, a 'this' scope, if you will, would be cool. Something like:

    Code:
    struct foo {
    void bar();
    void baz();
    };
    
    int main() 
    {
     foo object;
    
     this(object)
    {
     bar();
     baz();
    }
     return 0;
    }


    Anyway, one really easy way is to use a reference:



    Code:
    int main() 
    {
     foo object;
    
     foo & o = object;
    
     o.bar();
     o.baz();
    
     return 0;
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  9. #9
    Registered User
    Join Date
    Dec 2002
    Posts
    162
    Originally posted by Cat

    For a class filled with nothing but static functions (where you often call ClassName::Method(), etc.) put the methods in a namespace instead. [/B]
    hehe... thats the reason why I asked. I have static function member and it has a local pointer to its owner class. And so I want to skip writtin the pointer name all the time, just like a normal non-static function member. But how do I actually use namespace to achive this? If the function looked something like this (I am skipping the pointer assigning for simplicity):

    Code:
    void StaticFunction(void) {
    	Class *Parent // a pointer to the owner class
    
    	Parent->FuncMember();
    	Parent->VarMember = 1;
    }
    We haven't inherited Earth from our parents; instead we have borrowed her from our children - old Indian saying.

  10. #10
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    In that case, namespaces won't help. You can split control between two functions, however:

    Code:
    void Class::NotStaticFunction(void){
    	FuncMember();
    	VarMember = 1;
    }
    
    
    void StaticFunction(void) {
    	Class *Parent // a pointer to the owner class
    	Parent->NotStaticFunction();
    }
    I often do things like this when working with window procedures (which need to be static) that I want to access my window wrapper classes.

    There's still ONE call with the pointer dereferenced, but only one, and it's not so bad to deal with.

    All in all, if there was this odd kind of "using", it would only obfuscate code. As it is, I am one of the people who thinks "using" is not a good keyword to use, period. Trying to skip a few characters of typing, at the expense of code legibility, is not a good idea. In fact, were it not to try to improve backwards compatibility, I think Koenig lookup is all you'd ever need or want, and I think using would never have existed.
    Last edited by Cat; 08-15-2003 at 12:40 PM.

  11. #11
    Registered User
    Join Date
    Dec 2002
    Posts
    162
    Oh well, thanks anyway

    Although I think such a feature would make a significant improvement for reading (yes I belive so, becouse it would require less names on a code line and therefore save time) and specially writing code for big projects, if used professionally that is (without loops or conditions in-between). Some of you might say it would just be something for lazy programmers. But if you consider the fact that we (c/c++ programmers) use the block symbols when some other languages uses unique keywords to indicate the beginning and end of a block (loop/condition). In that case some could say (not me) that this method (using block symbols) is not very informative or is even a lazy way of writing code. Anyhow thanks again for the many replies

    Just to be clear:
    Code:
     {} = block symbols
    Last edited by Aidman; 08-15-2003 at 01:52 PM.
    We haven't inherited Earth from our parents; instead we have borrowed her from our children - old Indian saying.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 09:10 AM
  2. Specializing class
    By Elysia in forum C++ Programming
    Replies: 6
    Last Post: 09-28-2008, 04:30 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Creating a database
    By Shamino in forum Game Programming
    Replies: 19
    Last Post: 06-10-2007, 01:09 PM