class passing a pointer to self to other class

This is a discussion on class passing a pointer to self to other class within the C++ Programming forums, part of the General Programming Boards category; okay so I want to create a class that will keep track of all my objects automatically but don't know ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    13

    class passing a pointer to self to other class

    okay so I want to create a class that will keep track of all my objects automatically but don't know exactly how to accomplish this. my idea would be to creat a class that would have an array as a private member and then also have an add function that would take pointers of my obj. This class would be accessible by my other classes so when an obj is created ideally in its constructor it would pass a pointer of itself to the add function. I don't know if this is possible anyone know of a way to accomplish this?

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    easiest way is probably to use a vector and push_back your pointers into it in your add function.
    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

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    13
    okay i think i figured out how to do what i want using the this pointer though trying to use it i came across another thing i'd like to do. from cpprefrence.com i got the syntax for defining a class which is:
    Code:
      class class-name : inheritance-list {
      private-members-list;    
      protected:
      protected-members-list;
      public:
      public-members-list;
      } object-list;
    my question is, what is the syntax for the object-list which the site says: " [an] object-list can be used to immediately instantiate one or more instances of the class"

    anyone know how to use this feature?

  4. #4
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    class a {
    public:
      void foo() {}
    };
    
    class b:public a {
    public:
      void bar() {}
    } x, y;  // object list
    
    int main () {
      x.foo();
      y.bar();
    }
    Kurt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointer of array of class
    By 11moshiko11 in forum C++ Programming
    Replies: 5
    Last Post: 04-05-2008, 09:58 AM
  2. Compiler "Warnings"
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 04-24-2005, 01:09 PM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM

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