Thread: casting c++,const in class types,function return values...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    29

    casting c++,const in class types,function return values...

    hi guys!
    someone can tell me how can imakeke this thing:?
    suppose:
    class a
    {
    public:
    int b;
    a();
    }
    a::a(){};

    a my;
    float b;
    //how can I add a casting C++ manner to float wrapped type?
    //in practice : how can i perform this:
    b = float(my);
    ???
    someone can tell me why-download my program-:
    1) if i set NumperoOggetti as static-put this word in the NumeroOggetti class definition- the program can't resolve exteral link and gives so this error?
    how do i solve this problem? putting external somewhere ? where?
    2) if i set :
    class b
    {public:
    const int a;
    b();
    }
    b::b(){ a = 10; // it doesn't work,a const type value can't
    //be changed and so ,how do I initialize it?
    //puttng equal sign in the class this way: const int a = 10;
    //is not allowed eather. so???
    //another question:
    -suppose function:

    int * pluto()
    {
    int b=10;
    return &b
    }
    but the adress returned point to the stack?
    and so, once the function is executed the stack is popped and the address point to a "deallocated" value?
    thanks for any help...
    and if you send me an email response [email protected]
    THANKS

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    someone can tell me how can imakeke this thing:?
    suppose:
    class a
    {
    public:
    int b;
    a();
    }
    a::a(){};

    a my;
    float b;
    //how can I add a casting C++ manner to float wrapped type?
    //in practice : how can i perform this:
    b = float(my);
    ???
    One way to do this might be like so:

    Code:
    class a
    {
    public:
        int b;
        int operator()()
        {
            return b;
        }
        a();
    }
    
    a::a()
    {
    }
    
    a my;
    my.b = 45;
    float b = float(my());  // b should now equal 45.0
    But then again, if the data member is public, then can't you simply say something like float b = float(my.b); and be done with it.

    2) if i set :
    class b
    {public:
    const int a;
    b();
    }
    b::b(){ a = 10; // it doesn't work,a const type value can't
    //be changed and so ,how do I initialize it?
    //puttng equal sign in the class this way: const int a = 10;
    //is not allowed eather. so???
    For this you can initialize a const member of a class like this:
    Code:
    class b
    {
    public:
        const int a;
        b();
    };
    
    b::b() : a(10)
    {
    }
    Last edited by hk_mp5kpdw; 05-05-2004 at 11:10 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Should have put this all in one post but I wasn't sure what you were getting at with the other question so I skipped it at first...

    1) if i set NumperoOggetti as static-put this word in the NumeroOggetti class definition- the program can't resolve exteral link and gives so this error?
    how do i solve this problem? putting external somewhere ? where?
    Somewhere in your source file for the class you must do what is called explicit instantiation of that data member, i.e.

    Code:
    class a
    {
    public:
        static int iValue;
        static float fValue;
    };
    
    int a::iValue;  // You must explicitly instantiate every static class member like this
    float a::fValue;

    //another question:
    -suppose function:

    int * pluto()
    {
    int b=10;
    return &b
    }
    but the adress returned point to the stack?
    and so, once the function is executed the stack is popped and the address point to a "deallocated" value?
    thanks for any help...
    Yes... that's true, what's the question?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    As for the conversion problem, solve it like this:

    Code:
    class a
    {
      float operator float()
      {
        return b;
      }
    };
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  5. #5
    Registered User
    Join Date
    Apr 2004
    Posts
    29
    thank's guys

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  2. Weird errors.
    By Desolation in forum C++ Programming
    Replies: 20
    Last Post: 05-09-2007, 01:10 PM
  3. Another weird error
    By rwmarsh in forum Game Programming
    Replies: 4
    Last Post: 09-24-2006, 10:00 PM
  4. Why only 32x32? (OpenGL) [Please help]
    By Queatrix in forum Game Programming
    Replies: 2
    Last Post: 01-23-2006, 02:39 PM
  5. C++ FTP class won't work
    By lord mazdak in forum C++ Programming
    Replies: 8
    Last Post: 12-18-2005, 07:57 AM