Solved

Object serialization of nested class

Posted on 1998-04-10
4
652 Views
Last Modified: 2012-06-27
hello,
   I have a class X with a nested class Y. And I want to make class Y serializable. My code is
like this:

   class X : public CObject{
         class Y : public CObject{
                 DECLARE_SERIAL(Y)
         }
         DECLARE_SERIAL(X)
    }
    IMPLEMENT_SERIAL(X, CObject, 1)
    IMPLEMENT_SERIAL(X::Y, CObject, 1)

But error occurs during compiling:
error C2039: 'classX' : is not a member of 'Y'

Why?
0
Comment
Question by:syyung6
  • 2
4 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 1184184
Just a guess, but try
                     DECLARE_SERIAL(X::Y)

like
 class X : public CObject{
             class Y : public CObject{
                     DECLARE_SERIAL(X::Y)
             }
             DECLARE_SERIAL(X)
        }
        IMPLEMENT_SERIAL(X, CObject, 1)
        IMPLEMENT_SERIAL(X::Y, CObject, 1)
0
 
LVL 22

Expert Comment

by:nietod
ID: 1184185
Do you know for a fact that an MFC class can be declared within another class?  It might not be possible.  Although C++ allows it, it doesn't necessarily mean that MFC's macros are able to handle it.
0
 

Accepted Solution

by:
snemanov earned 200 total points
ID: 1184186
Hello syyung6,
There are some syntax typo (see differences):
   class X : public CObject{
   public:
         class Y : public CObject{
                 DECLARE_SERIAL(Y)
         };
         DECLARE_SERIAL(X)
    };
    IMPLEMENT_SERIAL(X, CObject, 1)
    IMPLEMENT_SERIAL(X::Y, CObject, 1)

The main problem of this code, MFC is not supported nesated classes. It's following from implementation _IMPLEMENT_RUNTIMECLASS and IMPLEMENT_SERIAL macros.

After long experience and tortures to use nested classes mechanism, I think it's silly idea.
Personally, I find no reasons to use in nested classes.

Best wishes,
               Serge
0
 
LVL 1

Expert Comment

by:issamwd
ID: 1184187
your code was:

   class X : public CObject{
         class Y : public CObject{
                 DECLARE_SERIAL(Y)
         }
         DECLARE_SERIAL(X)
    }
    IMPLEMENT_SERIAL(X, CObject, 1)
    IMPLEMENT_SERIAL(X::Y, CObject, 1)

             It is not accurate to define your classes like this you must define each class alone but,
you can derive one from the other but, I'll help you with your work.

   You can use another function which is SerializeClass() ; look carefully to the example at
the end of the page.
I'll try to give you the general form first,
the features of this member function are:

-CArchive::SerializeClass

void SerializeClass( const CRuntimeClass* pRuntimeClass );

-Parameters

pRuntimeClass   A pointer to a run-time class object for the base class.

-Remarks

Call this member function when you want to store and load the version information of a base
 class. SerializeClass reads or writes the reference to a class to the CArchive object, depending on the direction of the
CArchive. Use SerializeClass in place of ReadClass and WriteClass as a convenient way to
serialize base-class objects; SerializeClass requires less code and fewer parameters.

Like ReadClass, SerializeClass verifies that the archived class information is compatible with
your runtime class. If it is not compatible, SerializeClass will throw a CArchiveException.
Your runtime class must use DECLARE_SERIAL and IMPLEMENT_SERIAL;
otherwise, SerializeClass will throw a CNotSupportedException.


Use the RUNTIME_CLASS macro to retrieve the value for the pRuntimeClass parameter.
The base class must have used the IMPLEMENT_SERIAL macro.







I'll give you now a general example:
Example

class CBaseClass : public CObject { ... };
class CDerivedClass : public CBaseClass { ... };
void CDerivedClass::Serialize(CArchive& ar)
{
    if (ar.IsStoring())
    {
        //normal code for storing contents
        //of this object
    }
    else
    {
        //normal code for reading contents
        //of this object
    }

    //allow the base class to serialize along
    //with its version information
   
 ar.SerializeClass(RUNTIME_CLASS(CBaseClass));
    CBaseClass::Serialize(ar);
}

      So now  you can replace your code with the above code
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now