Thread: Is this legal C++

  1. #1
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072

    Is this legal C++

    This works in MSVC++ but not in Borland C++ Builder:

    Code:
    //Can contain a number, string or something
    class DataType
    {
    public:
    
       DataType( double num);
    
       //Other constructors
    
    private:
    
      //Contains type and data
    };
    
    
    DataType foo( )
    {
      double bar = 1;
    
      return bar;
    }
    Borland wants me to write

    return DataType(bar);

    According to the ANSI/ISO standard, is this required?
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  2. #2
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    Code:
    DataType foo( )
    {
      double bar = 1;
    
      return bar;
    }
    your return type here is DataType, but you're returning a double.

  3. #3
    Banal internet user
    Join Date
    Aug 2002
    Posts
    1,380
    lol.... er, I'm sorry

    Reading code back to yourself sometimes does help find errors...
    Code:
    Define function foo - no arguments with return type DataType
    Declare variable bar as double and set to 0
    Return from foo with bar
    I dunno, I've gotten used to reading my code, so the type mismatch sticks out to me

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    249

    Talking loool .... You remind me myself... lol

    One day,I did the same thing...

    That is a reg. programmer problem... it is ok ...
    C++
    The best

  5. #5
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    I know perfectly well what the problem is.

    But because DataType has a constructor that takes a double, MSVC++ does the conversion automatically. Borland does not.

    You can write:
    double a = 1.5;
    DataType d = a;
    There's no more ambiguity in writing
    return a;
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    249

    Red face Good for you

    Now you got it right...
    C++
    The best

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. 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
  4. is this legal?
    By Shadow12345 in forum C++ Programming
    Replies: 6
    Last Post: 04-16-2002, 12:44 PM