CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 38
  1. #1
    Join Date
    Jul 2008
    Location
    Ukraine, Kharkov
    Posts
    42

    'With' keyword in C++

    I used to write in VB for quite long and got sticked to a 'With' keyword, which helps to keep the code clean and clear. I wonder if there's a C++ equivalent of this keyword or probably another delicate way to keep order while coding. Sample:

    Code:
    class A
    {
    public:
    	int a;
    };
    class B
    {
    public:
    	A a;
    	int b;	
    };
    int main()
    { 
    	B ttt;
    	// With ttt.a
    	// a = 123;        
    	// End With	
    }
    Thanks!

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: 'With' keyword in C++

    Quote Originally Posted by Neolisk
    I used to write in VB for quite long and got sticked to a 'With' keyword, which helps to keep the code clean and clear. I wonder if there's a C++ equivalent of this keyword or probably another delicate way to keep order while coding.
    I advise against attempting to make C++ mimic another computer language.

    Use the proper (i.e. normal) C++ coding paradigms and syntax that all C++ programmers understand, and not try to make code look like another language's coding style.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Jul 2008
    Location
    Ukraine, Kharkov
    Posts
    42

    Re: 'With' keyword in C++

    Quote Originally Posted by Paul McKenzie
    not try to make code look like another language's coding style.
    I feel uncomfortable when I need to write like
    Code:
       this->btnButton->Text =
       this->btnButton->ForeColor =
       this->btnButton->BackColor =
    instead of (wish it worked this way)
    Code:
    with this->btnButton
    {
       Text =
       ForeColor =
       BackColor =
    }
    Isn't the last piece of code more clear and understandable?

  4. #4
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Post Re: 'With' keyword in C++

    I think there is a similar syntax in the latest C standard (not C++). In C++ you would write a constructor:

    Code:
    class B
    {
    public:
    
    // Constructor:
        B(int inB):a(),b(inB){}
    
    // Other functions ... 
    
    // Internal data:
    private:
        A a;
        int b;	
    };
    Code:
    int main()
    { 
    	B ttt(123);
    }
    Constructors are better because you can execute code inside them, for example allocate extra memory when the object is created and free that memory when the object is destroyed (by writing a destructor).
    Last edited by Zaccheus; July 23rd, 2008 at 03:19 AM.
    My hobby projects:
    www.rclsoftware.org.uk

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: 'With' keyword in C++

    Quote Originally Posted by Neolisk
    I feel uncomfortable when I need to write like
    Code:
       this->btnButton->Text =
       this->btnButton->ForeColor =
       this->btnButton->BackColor =
    instead of (wish it worked this way)
    Code:
    with this->btnButton
    {
       Text =
       ForeColor =
       BackColor =
    }
    Isn't the last piece of code more clear and understandable?
    Not to a C++ programmer, since there is no such thing as "with" in C++. That was my whole point from the beginning. It's the first version that's understandable to a C++ programmer.

    Use C++ syntax to do what you want.
    Code:
    SomeObject *p = this->btnButton;
    
    p->Text = whatever;
    p->ForeColor = whatever;
    p->BackColor = whatever;
    I have seen other attempts of making C++ look like other languages by using #define macros. They do nothing except obfuscate the actual code, as the program looks nothing like a C++ program.

    Regards,

    Paul McKenzie

  6. #6
    Join Date
    Jul 2008
    Location
    Ukraine, Kharkov
    Posts
    42

    Re: 'With' keyword in C++

    Quote Originally Posted by Zaccheus
    I think there is a similar syntax in the latest C standard (not C++). In C++ you would write a constructor
    I don't need to allocate/reallocate anything when I decide to shorten my code. It's just for visual simplicity. C code works for C++, so what's the syntax you mentioned?
    Quote Originally Posted by Paul McKenzie
    Use C++ syntax to do what you want.
    Seems like it's almost what I wanted. Well, now I need to get used to this. Thanks.

  7. #7
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: 'With' keyword in C++

    Quote Originally Posted by Neolisk
    I don't need to allocate/reallocate anything when I decide to shorten my code.
    The allocation I mentioned was just an example, the code I posted shows how your example would typically be done in C++.

    Quote Originally Posted by Neolisk
    It's just for visual simplicity. C code works for C++, so what's the syntax you mentioned?
    C and C++ are slowly moving apart, the latest C standard contains things which are not supported in C++.
    My hobby projects:
    www.rclsoftware.org.uk

  8. #8
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: 'With' keyword in C++

    My hobby projects:
    www.rclsoftware.org.uk

  9. #9
    Join Date
    Jul 2008
    Location
    Ukraine, Kharkov
    Posts
    42

    Re: 'With' keyword in C++

    Quote Originally Posted by Zaccheus
    the latest C standard contains things which are not supported in C++.
    Didn't know that, really. Then I think I'd better stick to Paul's suggestion, it's the most clear by now.

  10. #10
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: 'With' keyword in C++

    I've just noticed that you are talking about changing the properties of an object after the object had been created, sorry for my misunderstanding.
    My hobby projects:
    www.rclsoftware.org.uk

  11. #11
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: 'With' keyword in C++

    Except in a few isolated cases, the use of this-> is usually completely unnecessary.
    Code:
    this->btnButton->Text =
    this->btnButton->ForeColor =
    this->btnButton->BackColor =
    is the same as
    Code:
    btnButton->Text =
    btnButton->ForeColor =
    btnButton->BackColor =
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  12. #12
    Join Date
    Dec 2003
    Location
    Syracuse, NY
    Posts
    400

    Re: 'With' keyword in C++

    I have to agree with Paul. Don't try to mimic another language. I don't really use VB(I haven't used it for years) and I have no idea what 'with' is supposed to mean.

    All you have to do to change a public member is access it.
    in the case of your example you'd just have to do ttt.a.a = 323, provided the a class variable in B is valid. Alternatively you could make use of inheritance by making B a child class of A
    Code:
    class A
    {
    public:
        int a;
    }; 
    
    class B : public A
    {
    public:
        int b;
    };
    class B will still contain the int variable 'a' it just extends upon class A. The ': public A' just specifies that you want the class to be a parent of A with public access. Now class B would be able to access anything that is marked public or protected from A. So you can do:
    Code:
    int main()
    {
        B ttt;
        ttt.a = 32;
        return 0;
    }
    May not be super clean, but remember C++ is an intense language, as opposed to VB which is relatively more easily readable. Good thing though, you can hide your classes in header files and define the class's functions in C/C++ source files.
    "Windows programming is like going to the dentist: You know it's good for you, but no one likes doing it."
    - Andre LaMothe

    DLL For Beginners(UPDATED)
    Please use CODE tags
    Rate me if I've helped.

  13. #13
    Join Date
    Nov 2007
    Posts
    613

    Re: 'With' keyword in C++

    C++ is without With.

    If you think "this->btnButton->ForeColor = ..." is too long, you never saw a complex C++ source code.
    How about a pointer written on 2-3 lines ?

    You can simplify the writing by using something like:
    CButton* b = this->btnButton;

    then use:
    b->ForeColor = ...

    but this method will make the generated code longer and slower because a new pointer named b is created and used.
    Last edited by srelu; July 23rd, 2008 at 08:06 PM.

  14. #14
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128

    Re: 'With' keyword in C++

    Actually, I like comparing one programming language with another to understand their differences, strength and weakness. I agree that the "with" keyword will be very useful in the old time when everyone is still using edlin or vi. However, with today's IDE, it is not so important as most are equipped with intellisense and help to autocomplete our code. So it is less likely to misspell.

    Anyway, as already shown in Paul's excellent example, you can assign it to a temporary pointer to reduce typing.
    quoted from C++ Coding Standards:

    KISS (Keep It Simple Software):
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.

    Avoid magic number:
    Programming isn't magic, so don't incant it.

  15. #15
    Join Date
    Apr 1999
    Posts
    27,449

    Re: 'With' keyword in C++

    Quote Originally Posted by srelu
    You can simplify the writing by using something like:
    CButton* b = this->btnButton;

    then use:
    b->ForeColor = ...

    but this method will make the generated code longer and slower because a new pointer named b is created and used.
    It makes the code longer, but a good optimizer sees such things, so the code is more than likely not slower.

    Regards,

    Paul McKenzie

Page 1 of 3 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured