C++ question

Discussion in 'OT Technology' started by Joe_Cool, Jan 21, 2004.

  1. Joe_Cool

    Joe_Cool Never trust a woman or a government. Moderator

    Joined:
    Jun 30, 2003
    Messages:
    299,374
    Likes Received:
    580
    How much memory does a pointer to an object take up? Is it the size of the object, or just int? I'm working on a project that's going to have a variable number of objects of class foo, and I want to use new/delete to allocate them dynamically, and loop through them with an array of pointers.

    If I declaree my pointer array with 200 elements (foo* pf[200];)am I reserving 200 x sizeof(foo), or 200 x sizeof(int)?
     
  2. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    A 32-bit address is the same size as an int.
     
  3. SLED

    SLED build an idiot proof device and someone else will

    Joined:
    Sep 20, 2001
    Messages:
    28,118
    Likes Received:
    0
    Location:
    AZ, like a bauce!
    depends on the processor architecture, but it's usually the size. You can easily find out by doing printf("sizeof(int*) = %d\n", sizeof(int*));

    All pointers, are the same size, doesn't matter if you are referring to a foo* or an int*

    EDIT: was lazy on the printf example, fixed.
     
    Last edited: Jan 21, 2004
  4. Joe_Cool

    Joe_Cool Never trust a woman or a government. Moderator

    Joined:
    Jun 30, 2003
    Messages:
    299,374
    Likes Received:
    580
    Ok another question:

    Is it possible for a class 'foo' to have a member function that takes as an argument a pointer to an array of objects of type 'foo' and calls member functions of those objects?

    In other words, I'm trying to do something like this:

    Code:
    class foo
    {
      int a;
      foo();
      int geta();
      int show();
    }
    
    foo::foo()
    {
      a=1;
    }
    
    int foo::showa()
    {
      cout << "a:" << a << "\n";
    }
    
    foo::show(foo* a[])
    {
      for(int i=0;i<5;i++)
      {
         a[i]->showa();
      }
    }
    
    int main()
    {
      foo n[5];
      for(int i=0;i<5;i++)
      {
        n[i]=new foo();
      }
      foo[1]->show(foo);
    }
    I have similar code in a bigger & more complicated program I'm working on (just for personal interest - I'm not in school) and I have something like that on a larger scale. I keep getting a compile error (under linux using g++):
    request for member 'getm' in '*plan_arr' , which is of non-aggregate type 'planet*'

    What does that mean?
     
  5. Joe_Cool

    Joe_Cool Never trust a woman or a government. Moderator

    Joined:
    Jun 30, 2003
    Messages:
    299,374
    Likes Received:
    580
    When I separate the problem function so it's no longer part of the class, it compiles fine. Is the problem that it's sort of self-referential (class foo has a member that takes a pointer to object of type foo and accesses member functions)?
     
  6. crotchfruit

    crotchfruit Guest

    here is a better way to do it:

    Code:
    
    int foo::show(foo* a)
    {
      for(int i=0;i<5;i++)
      {
         a[i]->showa();
      }
    }
    
    
    int main()
    {
      foo* n = new foo[5]; 
      foo[1]->show(n);
    }
    
    of course this is still totally ugly (e.g. you should be passing in the length of array as a second argument to the function)..
     

Share This Page