?
Solved

Object serialization of nested class

Posted on 1998-04-10
4
Medium Priority
?
696 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 400 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

649 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