0

Say I have a class:

class foo
{
   public:
      foo();
      int len{return 1};

};

Then in main I have a class pointer:

foo *var[5];

My question is when do class pointers get initialized? Or do they not? How would one initialize them?

Edited by durpderp: n/a

3
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by durpderp
1

When you assign them memory or you may point then to an existing object which should already be initialized.

Your example 'class foo' returns len which does not exist....

Edited by gerard4143: n/a

0

Sorry about the example. I was just trying to write something quick and basic.
Excuse me if I'm being dense but what do mean by 'when you assign them memory'?

1

This line:

foo * var[5];

creates five pointers to a foo object, but that does not create five foo objects. var will hold 5 pointers to no-where or anywhere, which is the same.

To initialize five foo objects for those pointers to point to, you need to do this:

foo* var[5];
  for(int i=0;i<5;++i)
    var[i] = new foo;

Then you need to delete them with:

for(int i=0;i<5;++i)
    delete var[i];
0

Ok I get it now. I guess I need to brush up more than I thought :). Its been a while since I programmed C++. Thanks for the help everyone.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.