Results 1 to 14 of 14

Thread: How to avoid "class namespace" repetition?

  1. #1
    Join Date
    Oct 2013
    Posts
    102
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default How to avoid "class namespace" repetition?

    Hi there.

    I don't know how is actually called that thing, class prefix, namespace whatever... here is simple snippet:
    car.h
    Qt Code:
    1. class Car {
    2. public:
    3. Car();
    4. void TurnLeft();
    5. void TurnRight();
    6. };
    To copy to clipboard, switch view to plain text mode 
    car.cpp
    Qt Code:
    1. Car::Car() {
    2. //some constructor code
    3. }
    4. void Car::TurnLeft() {
    5. //some code
    6. }
    7. void Car::TurnRight() {
    8. //some code
    9. }
    To copy to clipboard, switch view to plain text mode 

    Now, how do I avoid repeating this "Car" prefix in constructor and method definitions? Is there some trick like:
    Qt Code:
    1. with ::Car{
    2. Car() {
    3. //some constructor code
    4. }
    5. void TurnLeft() {
    6. //some code
    7. }
    8. void TurnRight() {
    9. //some code
    10. }
    11. }
    12. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: How to avoid "class namespace" repetition?

    Google "using namespace"

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to avoid "class namespace" repetition?

    Quote Originally Posted by jefftee View Post
    Google "using namespace"
    I don't think that's what OP means. In his case Car is not a namespace but a class name.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to avoid "class namespace" repetition?

    There is no "with" in contemporary C++. AFAIK, only Zortech C++ knew "with". Bought, destroyed, forgotten. Requiescat in pacem. Also, I am not sure whether the "using" clause can help here. I recommend stop jerking and code car::car(). You will not do it too often, only once per method

  5. #5
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: How to avoid "class namespace" repetition?

    Quote Originally Posted by wysota View Post
    I don't think that's what OP means. In his case Car is not a namespace but a class name.
    wysota, you are correct as usual. That's what I get for sneaking time from work to respond to forum posts!

  6. #6
    Join Date
    Oct 2013
    Posts
    102
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to avoid "class namespace" repetition?

    Quote Originally Posted by jefftee View Post
    Google "using namespace"
    Well thanks, it looks like right direction but maybe it's not...I'm asking about how to remove prefixes "Car::" in my class definition. If I add Car namespace it wont compile, because it obviously can't be same as class name. If I use something other like "Carpark" I still have to use "Car::" prefix in front of method definitions, so it looks it's not what I want. Please be so kind to show it on my sample code, thanks again.

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to avoid "class namespace" repetition?

    Quote Originally Posted by Radek View Post
    I recommend stop jerking and code car::car(). You will not do it too often, only once per method
    One can put the whole code in a single file and then inline method bodies in class definitions
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to avoid "class namespace" repetition?

    You can use this simple macro to avoid repeating string "Car::" in the source:
    Qt Code:
    1. #define DECLARE_METHOD_FOR_CLASS_CAR(method) Car:: method ()
    2.  
    3. DECLARE_METHOD_FOR_CLASS_CAR(Car) {
    4. //some constructor code
    5. }
    6. void DECLARE_METHOD_FOR_CLASS_CAR(TurnLeft) {
    7. //some code
    8. }
    9. void DECLARE_METHOD_FOR_CLASS_CAR(TurnRight) {
    10. //some code
    11. }
    To copy to clipboard, switch view to plain text mode 
    Simple and beautiful.
    Or even more general:
    Qt Code:
    1. #define DECLARE_METHOD_FOR_ANY_CLASS(klass, method) klass :: method ()
    2.  
    3. DECLARE_METHOD_FOR_ANY_CLASS(Car,Car) {
    4. //some constructor code
    5. }
    6. void DECLARE_METHOD_FOR_ANY_CLASS(Car,TurnLeft) {
    7. //some code
    8. }
    9. void DECLARE_METHOD_FOR_ANY_CLASS(Car,TurnRight) {
    10. //some code
    11. }
    To copy to clipboard, switch view to plain text mode 

    I don't know why but I find this thread really funny, I never met any programmer bothered by this syntax

  9. #9
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: How to avoid "class namespace" repetition?

    You don't have to code the prefix at all if you use Qt Creator code refactoring. Add the method to your header file, right click on the line and then choose Refactor->Add definition in yourclass.cpp.

  10. #10
    Join Date
    Oct 2013
    Posts
    102
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to avoid "class namespace" repetition?

    Ok thank you all for suggestions, will try it.

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to avoid "class namespace" repetition?

    Quote Originally Posted by stampede View Post
    You can use this simple macro to avoid repeating string "Car::" in the source:
    I think your macro name is too short

    Anyway, your suggestion has a bug - it only works for methods without parameters.

    How about:

    Qt Code:
    1. #define DECLARE_METHOD_FOR_ANY_CLASS0(K, M) K::M()
    2. #define DECLARE_METHOD_FOR_ANY_CLASS1(K, M, A1) K::M(A1)
    3. #define DECLARE_METHOD_FOR_ANY_CLASS2(K, M, A1, A2) K::M(A1, A2)
    4. #define DECLARE_METHOD_FOR_ANY_CLASS3(K, M, A1, A2, A3) K::M(A1, A2, A3)
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. #12
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to avoid "class namespace" repetition?

    Now, how do I avoid repeating this "Car" prefix in constructor and method definitions?
    You can't really be saying that you are too lazy to type "Car::" when defining the class methods in the cpp file, are you? It's the language syntax, get used to it. Or as has been suggested, use a development environment that fills in the boilerplate for you.

    If this really isn't what you intended to ask, maybe you can rephrase the question.

  13. #13
    Join Date
    Oct 2013
    Posts
    102
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to avoid "class namespace" repetition?

    Quote Originally Posted by d_stranz View Post
    You can't really be saying that you are too lazy to type "Car::" when defining the class methods in the cpp file, are you? It's the language syntax, get used to it. Or as has been suggested, use a development environment that fills in the boilerplate for you.

    If this really isn't what you intended to ask, maybe you can rephrase the question.
    I do indeed. I'm new to C++ an Qt and I don't see a reason why wouldn't I be searching for simplifications, this is the process of learning. And you, despite your "Expert" title don't have any constructive suggestion I see, no offense, but you'd better be quite. I would close this thread now and thanks again all for suggestions.

  14. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to avoid "class namespace" repetition?

    I'm moving the thread into General Programming. It's not a Qt related question anyway...
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 3
    Last Post: 16th March 2015, 08:31
  2. Replies: 1
    Last Post: 5th May 2014, 08:49
  3. Using QEvent to avoid a slot to be called "concurrently"?
    By LisaDearheart in forum Qt Programming
    Replies: 4
    Last Post: 19th August 2012, 20:34
  4. How to avoid "warning: deprecated conversion from"?
    By Caius Aérobus in forum Qt Programming
    Replies: 3
    Last Post: 18th January 2011, 16:11
  5. Translation QFileDialog standart buttons ("Open"/"Save"/"Cancel")
    By victor.yacovlev in forum Qt Programming
    Replies: 4
    Last Post: 24th January 2008, 20:05

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.