Thread: Is it bad style of coding ??

  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,895
    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,895
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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,895
    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,895
    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.

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, 08:19 AM
  2. WS_EX_COMPOSITED style (double buffering) problems
    By JasonD in forum Windows Programming
    Replies: 2
    Last Post: 10-12-2004, 11:21 AM
  3. good news and bad news
    By Garfield in forum A Brief History of Cprogramming.com
    Replies: 25
    Last Post: 10-27-2001, 07:31 AM
  4. Efficient Coding Styles
    By ActionMan in forum C Programming
    Replies: 3
    Last Post: 10-04-2001, 09:48 PM
  5. coding style
    By ActionMan in forum Linux Programming
    Replies: 1
    Last Post: 10-03-2001, 07:36 AM