Solved

Object serialization of nested class

Posted on 1998-04-10
4
643 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Building cUrl in Windows v7.43.0 4 26
is twain_32.dll cmpatible with windows 10 ? 10 95
Dynamically allocate memory 9 47
C++ question 3 46
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

757 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

17 Experts available now in Live!

Get 1:1 Help Now