Thread: is this legal?

  1. #1
    Shadow12345
    Guest

    is this legal?

    two function PROTOTYPES

    int Area(int width, int = 1);
    and then
    int Area(int size);


    Now I asked some C++ genious why or why not this wouldn't work. First all this person said that you cannot assign to int and then he said some other mumbo jumbo. On the other hand I thought you didn't have to include parameter names in the parameters list, so although it says int = 1 i thought it would still be legal.

    Well any comments, criticisms and donations may be left here, thank you for joining this thread, please come again.

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    22
    im no genious but something seems wrong with int = 1

  3. #3
    Shadow12345
    Guest
    Well in my book (sams teach yourself C++ in 21 days second editionn [fifth is the newest]) it says you do not have to include paramater names in a function prototype, and you can assign a default value to the paramater if none is supplied by the calling function, hence you get the int = 1, which DOES look funny, but according to my ancient book is correctomundo.

    there is no spoon...

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571

    Re: is this legal?

    Originally posted by Shadow12345
    two function PROTOTYPES

    int Area(int width, int = 1);
    and then
    int Area(int size);


    Now I asked some C++ genious why or why not this wouldn't work. First all this person said that you cannot assign to int and then he said some other mumbo jumbo. On the other hand I thought you didn't have to include parameter names in the parameters list, so although it says int = 1 i thought it would still be legal.

    Well any comments, criticisms and donations may be left here, thank you for joining this thread, please come again.
    Perfectly legal, default assignment would be 1. I'm fairly sure.

  5. #5
    Davros
    Guest
    Both prototypes are perfectly legal, when used seperately.

    However, I would think the compiler would have problems resolving which function to called if you typed:

    int a = Area(5);

  6. #6
    Unregistered
    Guest

    Re: is this legal?

    Originally posted by Shadow12345
    two function PROTOTYPES

    int Area(int width, int = 1);
    and then
    int Area(int size);
    Code:
    int Area(int width, int MYNAME =1); //you can only assign a 
                                               //default value to a class constructor
                                               // and all pramaters must have a name
    int Area(int size);          // Is legal in c++ but not in c. ITs called
                                         //function overloading

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Now I asked some C++ genious why or why not this wouldn't work.
    >int Area(int width, int = 1);
    >int Area(int size);
    It won't work because of the default value you give to the first function. If you try to call the second function with an integer value then the compiler will have no idea which function to actually use since you can call the first function with one integer argument and the second is supplied by default. For example:
    result = Area ( i );

    This could be interpreted as either result = Area ( i, 1 ); or result = Area ( i );. This ambiguity will cause your compiler to choke. However, if you remove the default value for the first function everything will work fine:
    int Area(int width, int height);
    int Area(int size);

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What is legal and what is illegal?
    By dlwlsdn in forum C Programming
    Replies: 3
    Last Post: 11-14-2008, 12:48 PM
  2. Replies: 1
    Last Post: 04-03-2008, 01:17 AM
  3. Why this is legal to write uint_64(x)?
    By meili100 in forum C++ Programming
    Replies: 1
    Last Post: 11-21-2007, 05:10 PM
  4. Legal Question: May I put my name(e-mail link) on the interface of a product
    By zahid in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 06-17-2003, 11:34 PM