#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    4
    Rep Power
    0

    fun with pointers and more........


    Hi-
    O.K. I have a few questions. First, when one creates a object like so.....
    //****foo.h************
    class foo{
    foo();
    };
    //**********************

    //*****somefile.h******
    #include "foo.h"
    foo *bar;
    //**********************

    //*****somefile.cpp***
    #include "somefile.h"
    void main(){
    bar = new foo();
    }
    ************************
    The bar is a pointer to a foo object that can only be referenced
    through the pointer bar, right? Is this even close?

    Second can anyone tell me of a way to pass a pointer of object1
    into the constructor of object2 and then have a pointer of object1
    created in object2 that is set to referance the object1 that was passed
    too the constructor of object2 so that member functions in object2 can
    manipulate object1 ?
    after reading that it confused me here is some Pseudocode of what
    I want to happen .
    //********mainfunc*******************
    void main(){
    prt1_obj1 =new object1; //pointer to object
    object2(prt1_obj1); //pass pointer into object2
    }
    //declaration of class object2*******
    class object1;
    class object2{
    object1 **ptr2_obj1;
    object2(object1 ptr_obj1);
    foo()
    };
    //implementation of object2*********
    object2::object2(object1 ptr_obj1){
    ptr2_obj1=ptr_obj;
    }
    object2::foo(){
    prt2_obj1->func();
    }
    //******************************************
    // in the end Iwant the object that is created with and referenced
    //by prt1_obj1's function func to be executed.

    When I compiling I get this
    "prt2_obj1 which is of non-aggregate type object1"
    error and if prt2_obj1 is declare like so {object1 *ptr2_obj1;}
    I get this "multiple definition of prt2_obj1" from the linker ???

    I would like to thank anyone who bothered to read this
    and would like to praise anyone who can help!!!!!
    thanks again for your time
    Mileto->Frank
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    "The bar is a pointer to a foo object that can only be referenced through the pointer bar, right? Is this even clos?

    Yes, almost. You could do this:

    foo* bar2=bar;

    Then bar and bar2 both point to the foo object.

    "Second can anyone tell me of a way to pass a pointer of object1
    into the constructor of object2 and then have a pointer of object1
    created in object2 that is set to referance the object1 that was passed
    too the constructor of object2 so that member functions in object2 can
    manipulate object1 ?"


    Uhh...why wouldn't you just assign the address of the parameter in object2's constructor to object2's data member? There is no need for the constructor to create anything. For example,
    Code:
    object2(object1* ptr_obj1)
    {
          ptr2_obj1 = &ptr_obj1;
    }
    Better yet, why are you even using the type object1** for the data member of object2? Why do you need another level of indirection? Why not just make object2's data member of type object1*, and do this:
    Code:
    object2(object1* ptr_obj1)
    {
        ptr2_obj1 = ptr_obj1;
    }
    Last edited by 7stud; August 12th, 2003 at 03:21 AM.
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    4
    Rep Power
    0
    7stud: thanks
    not as complex as I thought. The linking error was from me initializing ptr2_obj1 outside of object2's class declaration.
    Sorry, with work and school 2am is the only time to code and a good time for stupid errors. thanks for the help and your time

    Mileto->Frank
  6. #4
  7. No Profile Picture
    Offensive Member
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2002
    Location
    in the perfect world
    Posts
    622
    Rep Power
    27
    here is another serious error!

    >>void main()

    an application must return 0 or an int error code to the OS upon termination or it is not compliant with the accepted standards.
    The essence of Christianity is told us in the Garden of Eden history. The fruit that was forbidden was on the Tree of Knowledge. The subtext is, All the suffering you have is because you wanted to find out what was going on. You could be in the Garden of Eden if you had just kept your f***ing mouth shut and hadn't asked any questions.

    Frank Zappa

IMN logo majestic logo threadwatch logo seochat tools logo