Is it bad style of coding ??

This is a discussion on Is it bad style of coding ?? within the C++ Programming forums, part of the General Programming Boards category; class Example has operator+= overloads. Example Class keeps a private vector of std::vector<X*> type that it updates with several functions ...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    219

    Is it bad style of coding ??

    class Example has operator+= overloads.
    Example Class keeps a private vector of std::vector<X*> type that it updates with several functions and operator overloads.
    Code:
    Example& operator+=(X* x);
    Example& operator+=(X& x);
    X is a Class that takes have constructor (not explicit) that takes int.
    Code:
    X* x1 = new X(1);
    X* x2 = new X(2);
    X x3(3);
    X x4 = 4;
    Example ex;
    ex += x1;
    ex += x2;
    ex += x3;
    ex += x4;
    So all this works as expected. But What I want to do is.
    Code:
    ex += 5;
    But it requires signature to be
    Code:
    Example& operator+=(const X& x);
    But I am storing non-const pointers in the vector.

    So What I did is I const_cast<X*>(x) it in operator+=(const X&)

    Now Is that a design flaw ??
    cause I am not maintaining the constness.

    actually I dont use const_cast much thats why I am asking is what I am doing assumed as bad style of coding ??

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    In any case operator+= should not modify the right-hand argument.

    It is hard to understand what you are doing (IMO, overloading the same operator for pointers and objects is strange too): are you taking the address of the object passed in and storing it somewhere? In which case are you sure that you aren't eventually storing pointers to objects that have a short lifetime?

    Would this work?
    Code:
    Example ex;
    for (int i = 0; i != 10; ++i) {
        X x(n);
        ex += x;
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    const_cast is bad... Why would you even want it? I assume that the operator+= does not modify it's argument. Could you paste the operator+= code, maybe there's something bad in there.

    So yeah, make it const. If you call a function on the object, you'll want that function to be const to, like this:
    int getSomething() const;

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    That is absolutely broken. You're storing a pointer to a temporary, and your program will crash or misbehave sooner or later.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    My intension is to do ex += 5.

    so what if I do accept const X& x as arguments and pass it as

    X* __tmp = new X(x);
    l.oush_back(__tmp);

    I don't this these dangaling problems would appear.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    True. Of course, then there's also no problem with the const.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,157
    However, unless you are writing a compiler or standard library implementation, it is a bad coding practice to use __tmp as a variable name since identifier names that contain consecutive underscores are reserved to the implementation for any use.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    Ya I now that.

    However one thing I would like to make sure

    when an application exists does all Operating System cleans the garbage memory(e.g. new Objects on which delete has not been used) accuired by it automatically ??

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,157
    when an application exits does all Operating System cleans the garbage memory(e.g. new Objects on which delete has not been used) accuired by it automatically ??
    No, but many of the modern ones do.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    so any based on Linux kernal 2.6+ , Mac, windowx Xp+ offers this features ??

  11. #11
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    But If I code like this a different problem will appear if I do this. although the problem is not so critical or important.

    it is when a pointer is supplied its the vector content is linked with the actual content when a reference is supplied the vector content is also linked with the original one.
    But when a const reference is supplied its clone is there on vector content.

  12. #12
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Yep. But how do you expect to link against the literal 5?

    And yes, all Linux (even kernel 1.0), Mac and Windows NT and Windows 9x series OSs free an application's resources when it exits.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  13. #13
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    I dont expect a link against literal 5
    but not only literals require const X& during += operator overload
    But If somebody does like.

    const X& y = SOMETHING;
    Example ex;
    ex += y;

    then somebody e.g. the user of tsih Example Class might expect that internal vector holds a link to y.

  14. #14
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Perhaps.

    But this is the point where I start questioning your motive in writing this class, so it's hard for me to understand your envisioned usage problems.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  15. #15
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    Ok I think these few Lines of Code will explore What I am trying to do.

    Code:
    Js::Array ex;
    ex += Js::Number(2);
    ex += Js::String("Hello");
    ex += "Hello World"
    ex += 20;
    += is a testing operator I might be using the same technique on operator [] too.

    Its a CGI Application where Js::Array will be commited to client side and write a Javascript Array.

    **I belief that code for a CGI application should be easy and flexible so one of my target is syntactic ease and flexibility.
    Last edited by noobcpp; 11-06-2008 at 10:25 AM.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Your Coding Style?
    By Krak in forum A Brief History of Cprogramming.com
    Replies: 45
    Last Post: 06-02-2005, 09:19 AM
  2. WS_EX_COMPOSITED style (double buffering) problems
    By JasonD in forum Windows Programming
    Replies: 2
    Last Post: 10-12-2004, 12:21 PM
  3. good news and bad news
    By Garfield in forum A Brief History of Cprogramming.com
    Replies: 25
    Last Post: 10-27-2001, 08:31 AM
  4. Efficient Coding Styles
    By ActionMan in forum C Programming
    Replies: 3
    Last Post: 10-04-2001, 10:48 PM
  5. coding style
    By ActionMan in forum Linux Programming
    Replies: 1
    Last Post: 10-03-2001, 08:36 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21