Thread: what is not good in this function?

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    103

    what is not good in this function?

    Code:
    void set(int newprice=-1,char newname[]={"NONAME"},int newqty=-1)
            {
                if(price!=-1)
                price=newprice;
                if(strcmp(newname,"NONAME")!=0)
                strcpy(name,newname);
                if(qty!=-1)
                qty=newqty;
            }
    in the above code .... what is not a good habit or illegal
    i get a warning like this......

    [Warning] extended initializer lists only available with -std=c++11 or -std=gnu++11 [enabled by default]

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Code:
    {"NONAME"}
    is an array of strings containing a single string, AFAIK (unless using C++11 initializer lists).
    It should just be "NONAME".

    What is name, a global variable?
    And why are you using C strings?
    Unless there is some restriction, you'll find std::string s easier to use. (for example, you can assign instead of strcpy).

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    103
    this is a class member function and the undeclared variables are declared in the class(pvt)

  4. #4
    Registered User
    Join Date
    Apr 2013
    Posts
    103
    there's restriction in my school....they cannot change borland TC++(they are like monkeys who still want to travel thousands of miles bare-foot while car is invented).
    but i use modern compilers and editors now at my home.
    Last edited by Mukul Kumar; 11-22-2013 at 10:00 AM.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    The indentation is not good, the whitespace is not good, and the fact that it does three separate things is not good.

    In addition to what has been pointed out, your second argument should also be const.

    What is the point of having default arguments on all parameters when calling it with no arguments will mean it simply achieves nothing other than wasting CPU cycles?

    Why would you not simply have 3 different setters?
    The fact that you're using setters in the first place is a code smell. Can you not just pass these things to the constructor?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Good single character option function
    By eponymous in forum C Programming
    Replies: 12
    Last Post: 03-15-2008, 06:17 PM
  2. Good old 'Swap' function
    By starcatcher in forum C Programming
    Replies: 29
    Last Post: 02-03-2008, 10:38 AM
  3. Good function libraries?
    By SoulBlade in forum C++ Programming
    Replies: 4
    Last Post: 01-19-2006, 09:17 PM
  4. Pointer to function to handle event.Is a good idea?
    By giannirossi in forum C++ Programming
    Replies: 3
    Last Post: 05-31-2005, 01:12 AM
  5. Replies: 13
    Last Post: 04-29-2002, 11:06 PM

Tags for this Thread