Is it always required to derive a class that has member which needs to dynamically allocated from CBase

Login to reply to this topic.
Mon, 2008-03-10 12:14
Joined: 2006-12-19
Forum posts: 72

Hi,

I want to know "Is it always required to derive a class that has member which needs to dynamically allocated from CBase"?

or something like the following code is OK?

#define MAX_ELEMENT     10
#define MAX_ATTRIBUTE   4
struct Attribute
{
        HBufC8 *AttributeName;
        HBufC8 *AttributeValue;
};
typedef Attribute SAttribute;

class CXMLElement
{
        HBufC8* ElementName;
        SAttribute attribute[MAX_ATTRIBUTE];
        int AttributeCount;
private:
        CXMLElement();
        void ConstructL();
public:
        static CXMLElement* NewL();
        static CXMLElement* NewLC();
        ~CXMLElement();
        void SetElementNameL(const TDesC8& EName);
        TPtr8 GetElementName();
        void AddAttributeL(const TDesC8& AName ,const TDesC8& AValue);
        TPtr8 GetAttributeName(int index);
        TPtr8 GetAttributeValue(int index);
        TInt GetAttributeCount();
};

Thanks for your patience reply.
Regards,
NN


Mon, 2008-03-10 12:39
Joined: 2004-07-17
Forum posts: 110
Re: Is it always required to derive a class...

No it's not OK. Derive your C classes from CBase. Why would you want to do anything else!?

Mon, 2008-03-10 18:45
Joined: 2007-09-23
Forum posts: 149
Re: Is it always required to derive a class that has

" "Is it always required to derive a class that has member which needs to dynamically allocated from CBase"?"

The English isn't quite clear in your question, what was your doubt about the member part of your question?

In your example code, CXMLElement contains a data member that allocates memory ElementName (which should be called iElementName to follow naming conventions). If you have a class which allocates memory then that class must derive from CBase, so CXMLElement must derive from CBase.

Tue, 2008-03-11 05:16
Joined: 2006-12-19
Forum posts: 72
Re: Is it always required to derive a class that has member whic

Thanks fig7 and Numpty Alert.
Despite of my bad english (i m improving it)u have answered the question.
This is what I wanted to know.

Tue, 2008-03-11 17:43
Joined: 2005-12-07
Forum posts: 56
Re: Is it always required to derive from CBase

CBase classes has two advantages:
1- it has a virtual distructor
Advantage: In this case when there is a derived class of CXMLElement then objects will be cleaned properly (Base class objects will be cleaned properly)
2- It has a separate "new" function which allocates & initializes all data member to NULL.
Advantage:
Suppose your class is not derived from CBase then:
class CXMLElement
{
HBufC8* ElementName;
public:
static CXMLElement* NewL()
{
CXMLElement*self = new (ELeave) CXMLElement( );
CleanupStack::PushL( self );
self->ConstructL();
CleanupStack::Pop();
return self;
}

static CXMLElement* NewLC();
~CXMLElement()
{
delete ElementName;
}

private:

void ConstructL()
{
HBufC8* ElementName = HBufC8::NewL(100);
/// some other processing which might leave......
}

};

Now lets suppose there is a Leave in ConstructL then ~CXMLElement() will NOT be called since it is not Derived from CBase. only cells wiill be deleted for CXMLElement object. so now there is a memory Leak of ElementName.

And since CBase initializes class data member to NULL. there is no chance of deleting a Garbage location. This might happen in partially constructed object.

In short CBase does few hacks that we might forget to do, so its better to derive classes from it (where there is some dynamic allocation)

Please correct me if i am wrong.

Tue, 2008-03-11 18:19
Joined: 2007-09-15
Forum posts: 61
Re: Is it always required to derive a class that has...

I think CBase classes initialize its data members to 0 rather than to NULL.

Tue, 2008-03-11 19:20
Joined: 2007-09-23
Forum posts: 149
Re: Is it always required to derive a class that has

Talking about it having advantages and saying "so its better to derive classes from it " you make it sound like you have a choice, you don't - you must derive from CBase in the situations mentioned if you want to follow the conventions and standards.

Wed, 2008-03-12 05:44
Joined: 2006-12-19
Forum posts: 72
Re: Is it always required to derive a class that has member whic

Thanks 83.Manish and All for making things more clear.

When Leave occurs it will not be tranfered to the framework if we dont derive the CXMLElement class form CBase.So only memebers from the CXMLElement object will be deleted but not the heap allocated object.

Regards,
Netra

  • Login to reply to this topic.