theoretical problem

This is a discussion on theoretical problem within the C++ Programming forums, part of the General Programming Boards category; i haven't gotten rest (or any c++ coding done) in days so this is probably real easy, look at the ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    403

    theoretical problem

    i haven't gotten rest (or any c++ coding done) in days so this is probably real easy, look at the following psuedo-code:


    class cppclass{
    private:
    int *b;
    public:
    cppclass(int * passedpointer);
    };


    //global pointer
    int *a;

    int main(void)
    {
    cppclass foobar;
    ...
    a = functionthatsetsa(); //now a is set
    ...
    foobar(a); //so now foobar contains a member (b) which is a
    }




    //any calls to foobar.b fail, several runs through the program show that a is set and that somehow my constructor isn't setting b to point to the same thing that a is



    (hey and by the way, i haven't been on the boards in a while because school just started and i started dating this girl and i haven't had an ounce of time to myself until now)

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    403

    nice, i forgot to show my constructor

    the constructor basically does this:

    cppclass::cppclass(int * passedpointer)
    {
    b = passedpointer;
    }

  3. #3
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    You have no default constructor, but you have tried to call one and you can't use the constructor once the class instance has already been constructed. If you want to compile what you've done you'll have to provide a default argument and overload the () operator -

    Code:
    class cppclass{ 
    private: 
        int *b; 
    
    public: 
        cppclass(int * passedpointer=0){b = passedpointer;}
        void operator ()(int* &c){b=c;}
    }; 
    
    
    //global pointer 
     int *a; 
    
    int main(void) 
    { 
        cppclass foobar; 
    
        int b =10;
        a =&b; 
     
        foobar(a); //so now foobar contains a member (b) which is a 
    
        return 0;
    } 
    

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    403

    thanks for the reply that was a problem but not what i was looking for

    haha sorry about that, bad psuedocode, as i said im tired. but what i was doing was actually

    cppclass foobar(a); where i had written cppclass foobar;

    sorry about that, i'm thinking in like 3 languages at once here and sometimes stuff that's wrong in all of them comes out.

    i should also the problem is at run time so it isn't any sort of obvious error like that, it has something to do with my pointer copying.

  5. #5
    edk
    edk is offline
    Registered User
    Join Date
    Sep 2001
    Posts
    20
    be sure to write a copy constructor as well ... if you don't write a copy constructor, i don't think the compiler will provide you with one. if you're passing by value to a function, you're essentially making a copy of the value you pass to a function; if there is not a copy constructor defined, you can't make a copy, and the compiler chokes. however, if you pass by reference, you don't need a copy constructor. but, then you run the risk of modifying the actual value unintentionally.
    liberate tutamet ex inferis

  6. #6
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    however, if you pass by reference, you don't need a copy constructor. but, then you run the risk of modifying the actual value unintentionally.
    you can get the best of both worlds by passing a const reference.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  7. #7
    Registered User
    Join Date
    Aug 2001
    Posts
    403

    ok a rewording

    i have a pointer that is built to modify a "surface" (actually a matrix but it's not needed to know for this) and i want a class to also modify this surface, so that class has to have the same pointer to modify the surface, how do i do this?

  8. #8
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    I'm not sure where you're having problems but if you want the pointer in your class to be a reference to your global pointer, then you'll have to have the reference to a pointer in your class. This way if your global pointer is set to point at anything else, so will your class pointer -

    Code:
    class cppclass{ 
    private: 
        int *&b; 
    
    public: 
        cppclass(int *&passedpointer):b(passedpointer){}
        
    }; 
    
    
    //global pointer 
     int *a; 
    
    int main(void) 
    { 
        int b =10;
        a =&b;
        cppclass foobar(a); 
            
        int c = 50;
        a=&c;   //foobar::b now points to int c
    
    
        return 0;
    }

  9. #9
    Registered User
    Join Date
    Aug 2001
    Posts
    403

    thanks, that was close enough for me to figure out what i needed

    and sorry about my lack of ability to explain, i am extremely worn out by the time i get to this board

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 05:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 09:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21