Omgili, forum search, forums search, search forums, discussion search,discussions search, search discussions, board search, boards search, search boards
  Advanced Search

About instantiation of an abstract class

On Sat, 18 Apr 2009 17:30:00 +0800, "Jack" <...@knight.com

Dear all,
If I have something like the following,

Mtl* pkMtl = pNode-
where GetMtl() is pure abstract
like
class INode {

virtual Mtl* GetMrl() = 0;
};

When I called GetMtl() in the statement above,
pkMtl became NULL.

I have been thinking about writing
Mtl* pkMtl = new Mtl();
pkMtl = pNode-
but the compile doesn't allow me to do this..
Sorry, as I have forgotten about this academically...
Could you please refresh my mind on how to go about instantiating a Mtl
Object?
Sorry if it sounds stupid...
Thanks a lot
Jack



On Sat, 18 Apr 2009 11:57:02 +0200, "Giovanni Dicanio" <...@REMOVEMEgmail.com

"Jack" <...@TK2MSFTNGP03.phx.gbl...

pNode should point to an instance of some class derived from INode, that
implements the GetMtl() method (which is declared as pure virtual in INode).

Have you tried following the code step-by-step using a debugger?

This will cause a leak (memory leak, and in general leak of resources
allocated in Mtl constructor), beacuse in the first statement you allocate a
new instance of Mtl() on the heap, but then in the second statment you loose
the pointer pkMtl returned by new (because you overwrite it with the return
value of pNode-
Giovanni


On Sat, 18 Apr 2009 18:16:25 +0800, "Jack" <...@knight.com

What really confused me was the following code block originated from
http://www.geometricaltools.com

[code]

Wm3::Spatial* WSceneBuilder::BuildMesh (INode* pkMaxNode,
Wm3::Spatial* pkWm3Node)
{
// Convert a Max trimesh to one or more equivalent Magic trimeshes.
//
// pkMaxNode:
// Mesh node in the Max hierarchy.
// pkWm3Node:
// Parent node in Magic scene to which newly created.
// Returns pointer to new child node in the Magic scene. This will
point
// directly to a trimesh object, if there is only one Magic mesh, or to
// a "link" node, whose children are the mulitple trimeshes needed to
// represent the Max mesh.

bool bNeedDel = false;
TriObject* pkTriObj = GetTriObject(pkMaxNode,&bNeedDel);
if ( !pkTriObj )
return NULL;
Mesh* pkMaxMesh = &pkTriObj-
Mtl* pkMtl = pkMaxNode- assert( pkMtl );
int iMtlID = m_kMtlList.GetID(pkMtl);
assert( iMtlID [/code]
[snip]

The line
Mtl* pkMtl = pkMaxNode-returns NULL and assigned to pkMtl.

I left this code snippet intact after applying the application (plugin)
wizard which came with the package.
So it assume the original source was correct...
Finally it came up with 0x00000000 on that line. And I couldn't get out of
it.
:) Sorry, must be drained after working a week straight...
Thanks
Jack




On Sat, 18 Apr 2009 18:44:30 +0800, "Jack" <...@knight.com

Oops, I think you wouldn't be able to compile it....
Because you need to have max sdk
Thanks
Jack


On Sat, 18 Apr 2009 07:00:55 -0400, David Wilkinson <...@effisols.com

Jack:

I do not see

Mtl* pkMtl = pkMaxNode-
in this code. I see

Mtl* pkMtl = pkMaxNode-
I assume this is a typo.

Whether or not pkMt1 is NULL has nothing to do with the fact that Mt1 is an
abstract class, nor to do with the code you showed here, but rather with what
has previously been placed in the object pointed to by pMaxNode. Clearly the
author of this method expected pkMt1 to be non-NULL. If you are seeing NULL, I
would say you are not using the GeometricalTools library (or whatever it is)
correctly.

As noted by Giovanni, if Mt1 is abstract and pkMt1 is non-NULL, then it should
(must) be pointing to an instance of a concrete class derived from Mt1.

--
David Wilkinson
Visual C++ MVP

On Sat, 18 Apr 2009 19:09:58 +0800, "Jack" <...@knight.com

[snip]

Hi David,
Thanks for your suggestions. Maybe I have to look into other possibilities
of the error...Maybe time for a break, been working on it for a week, still
couldn't figure out where the problem was.
Thanks
Jack


On Sat, 18 Apr 2009 12:48:32 -0500, "Doug Harrison [MVP]" <...@mvps.org

On Sat, 18 Apr 2009 17:30:00 +0800, "Jack" <...@knight.com

I'm not familiar with the term "pure abstract". The class INode has a /pure
virtual function/, which makes INode an /abstract class/.

What would be the point if it did allow you to do it? Deliberately create a
memory leak? I mean, you would be creating an object and then immediately
overwriting the only pointer to it you have.

You can't instantiate abstract classes. That's why they call them
"abstract". This means you can't create an INode, which is abstract, and is
presumably the reason your attempt to create an Mtl failed. If
INode::GetMtl() returns NULL, you need to understand the semantics of using
that function. It's got nothing to do with abstract classes.

--
Doug Harrison
Visual C++ MVP

On Sun, 19 Apr 2009 13:31:02 +0800, "Jack" <...@knight.com

Thanks Doug
Jack