TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: Neil Heller
from: Pontus Goffe
date: 2003-09-08 20:29:42
subject: `new`ing pointers

Hi Neil !

07-Sep-03 15:00:00, Neil Heller wrote to All
          Subject: "new"ing pointers

// Yes, derriving would make it possible to access protected members.
// In this case I wouldn't.

class Node
{
public:
        Node(int nSizeToAllocate);
private:
        char* SomeString;   // length determined at run-time

public:
        void SomeFunction();
};

Node::Node(int nSizeToAllocate)
        :
        SomeString(NULL)
{
        SomeString = new char[nSizeToAllocate];
}

void linked_list::build_it()
{
        ANode = new Node(CalculatedNumber);
}

// Allocating in the constructor is one way of doing it,
// but not a recommended one. Instead one could
class Node
{
public:
        Node();
private:
        char* SomeString;   // length determined at run-time

public:
        void SomeFunction();
        bool MakeString(int nSizeToAllocate);
};

Node::Node()
        :
        SomeString(NULL)
{
}

bool Node::MakeString(int nSizeToAllocate)
{
        if (SomeString)
        {
                // already allocated
                return false;
        }
        SomeString = new char[nSizeToAllocate];
        if (SomeString)
        {
                return true;
        }
        return false;
}

void linked_list::build_it()
{
        ANode = new Node();
        if (ANode->MakeString(CalculatedNumber))
        {
                // whatever
        }
        else
        {
                // die
        }
}

//PG
   8-Sep-2003  20.29
 
--- Terminate 5.00/Pro 
* Origin: Pompes point (2:204/254.33)
SEEN-BY: 633/267 270
@PATH: 20/11 106/1 2000 633/267

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.