GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 22-Aug-2008, 20:08
LostPointer LostPointer is offline
New Member
 
Join Date: Aug 2008
Posts: 4
LostPointer is on a distinguished road

need a quickie, initializers with dynamic allocation...


If I have this class...

CPP / C++ / C Code:
class Object
{
public:
    Object(void);
    Object(const unsigned input);
    virtual ~Object(void);

   Object(const Object&copy);
   Object&operator=(const Object&rhs);

private:
    unsigned integer;
};


Object::Object(void)
{
}

Object::Object(const unsigned input) : integer(input)
{
}

// etc...

Can a do a dymanic allocation with the initializers...

CPP / C++ / C Code:
int main()
{
    Object *obj = 0;

    obj = new Object[2]{0,1};
// allocation for two instances of Object with the two initializers.

// etc...

    if (obj != 0) delete [] obj;
    obj = 0;


    return 0;
}

Thank you!

Regards.
  #2  
Old 22-Aug-2008, 20:32
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: need a quickie, initializers with dynamic allocation...


Quote:
Originally Posted by LostPointer
Can a do a dymanic allocation with the initializers...
No, elements of an array always call the default constructor. Consider the following:
CPP / C++ / C Code:
#include <iostream>

using namespace std;

class foo {
public:
    foo()  { cout << "foo::foo():\t" << (void*) this << endl; }
    ~foo()  { cout << "foo::~foo()\t" << (void*) this << endl; }
};

int
main() {
    foo a[2];

    return 0;
}
If you must initialize each element within an array, you might consider creating an array of pointers, & call whatever constructor you choose.
  #3  
Old 22-Aug-2008, 20:45
LostPointer LostPointer is offline
New Member
 
Join Date: Aug 2008
Posts: 4
LostPointer is on a distinguished road

Re: need a quickie, initializers with dynamic allocation...


thanks OCICAT for the quick response.

That's what I believed to be true.
I was hoping for a different answer, but I read that else where and just wanted to be sure.
I know there will only be two instances of the Object at runtime so I'm doing an array of two, but I kind of wanted to control the destruction of the instance.

Thank you.

Regards.
  #4  
Old 22-Aug-2008, 21:07
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: need a quickie, initializers with dynamic allocation...


Quote:
Originally Posted by LostPointer
... but I kind of wanted to control the destruction of the instance.
...then creating an array of Object* elements will allow you to control destruction at your choice.
  #5  
Old 22-Aug-2008, 21:23
LostPointer LostPointer is offline
New Member
 
Join Date: Aug 2008
Posts: 4
LostPointer is on a distinguished road

Re: need a quickie, initializers with dynamic allocation...


...okay?, how do I destory just one of the two instances? using this example.

CPP / C++ / C Code:
int main()
{
    Object obj[2] = { Object(0), Object(1) };
// array of two instances of object with initializers.

// etc...

// destroy one of the instances???


    return 0;
}

Thank you.

Regards.
  #6  
Old 22-Aug-2008, 21:54
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: need a quickie, initializers with dynamic allocation...


Quote:
Originally Posted by LostPointer
...okay?, how do I destory just one of the two instances?
Again, you cannot uniquely initialize class instance elements within an array; each element will only call the default constructor. As I recall, the reason this restriction was put into the standard was for performance reasons.

So if you want to call different constructors for the various elements within an array, you can get around the above restriction by creating an array of class instance pointers.

Since you ask how to uniquely destroy objects at will (which is unrelated to array initialization...), consider the following example which mixes both concepts of unique constructor calls for array elements & absolute control over object destruction:

CPP / C++ / C Code:
#include <iostream>

using namespace std;

class foo {
    int v;
public:
    foo(int i)  { v = i;  cout << "foo::foo(" << v << "):\t" << (void*) this << endl; }
    ~foo()  { cout << "foo::~foo(" << v << ")\t" << (void*) this << endl; }
};

void f(foo *a[]);

int
main() {
    foo *a[2];
    a[0] = new foo(2);
    a[1] = new foo(4);

    f(a);

    return 0;
}

void
f(foo *a[]) {
    cout << "entering f()" << endl;

    delete a[0];
    a[0] = 0;

    cout << "leaving f()" << endl;
}
In function f(), a[0] does not have to be set to 0, but destruction will not change the address assigned to the array element. Bad things can happen if you don't keep track of what instances have been destroyed.

Since the language standard does not define what the behaviour should be if the destructor is called on the same address multiple times (In other words, what will happen if I call the destructor multiple times on the same address?), the behaviour seen with one vendor's compiler is not guaranteed with another. The safe solution is to set the address to 0 after destruction as the behaviour of deleting an address value of 0 is well defined -- nothing.
  #7  
Old 22-Aug-2008, 22:13
LostPointer LostPointer is offline
New Member
 
Join Date: Aug 2008
Posts: 4
LostPointer is on a distinguished road

Re: need a quickie, initializers with dynamic allocation...


That's it, that will work prefectly with what I have.
I made the changes and now its controllable!

Thank you OCICAT.

Regards.
 
 

Recent GIDBlogProblems with the Navy (Chiefs) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
custom array class: operator[] not working for new dynamic allocation transfrank C++ Forum 3 06-Sep-2007 16:34
Dynamic 2D Array pointers, Allocation, and input Assiral C Programming Language 3 27-Oct-2006 14:44
Help with OOP classes and Dynamic Allocation noob2c++ C++ Forum 3 18-Sep-2006 18:41
Dynamic memory allocation - Dangling pointers revisited karthikeyansen C++ Forum 13 27-Jul-2005 16:30
3D array dynamic memory allocation cjwatchdog C Programming Language 3 20-Feb-2004 16:27

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 07:24.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.