Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Convert from CObject to inherited class

Convert from CObject to inherited class

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
10 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bugDanny
    wrote on last edited by
    #1

    I'm trying to use CObList with a class I created. The class I created I inherited CObject. class CFAFRecord : public CObject {/*data*/} I'm able to create this and insert it into the CObList. But when I try to access the list with m_variable.GetNext(m_curPosition); and I try to assign it to a CFAFRecord object, I get the error, 'cannot convert 'class CObject' to 'class CFAFRecord'. It won't let me typecast it. It looks like it's not recognizing that CFAFRecord is a CObject. Any help here? Danny The stupidity of others amazes me!

    P J R 3 Replies Last reply
    0
    • B bugDanny

      I'm trying to use CObList with a class I created. The class I created I inherited CObject. class CFAFRecord : public CObject {/*data*/} I'm able to create this and insert it into the CObList. But when I try to access the list with m_variable.GetNext(m_curPosition); and I try to assign it to a CFAFRecord object, I get the error, 'cannot convert 'class CObject' to 'class CFAFRecord'. It won't let me typecast it. It looks like it's not recognizing that CFAFRecord is a CObject. Any help here? Danny The stupidity of others amazes me!

      P Offline
      P Offline
      PJ Arends
      wrote on last edited by
      #2

      First bit of advice is to drop CObList and use STL's std::list instead. If you can not do that and have compeling reasons to use CObList maybe you could post some code showing how you are trying to cast the returned pointer.


      "You're obviously a superstar." - Christian Graus about me - 12 Feb '03 "Obviously ???  You're definitely a superstar!!!" - mYkel - 21 Jun '04 "There's not enough blatant self-congratulatory backslapping in the world today..." - HumblePie - 21 Jun '05 Within you lies the power for good - Use it!

      B 1 Reply Last reply
      0
      • B bugDanny

        I'm trying to use CObList with a class I created. The class I created I inherited CObject. class CFAFRecord : public CObject {/*data*/} I'm able to create this and insert it into the CObList. But when I try to access the list with m_variable.GetNext(m_curPosition); and I try to assign it to a CFAFRecord object, I get the error, 'cannot convert 'class CObject' to 'class CFAFRecord'. It won't let me typecast it. It looks like it's not recognizing that CFAFRecord is a CObject. Any help here? Danny The stupidity of others amazes me!

        J Offline
        J Offline
        John M Drescher
        wrote on last edited by
        #3

        Although I have used MFC's template classes for years in the past, I second the advice above. Take a look at stl. John

        T 1 Reply Last reply
        0
        • J John M Drescher

          Although I have used MFC's template classes for years in the past, I second the advice above. Take a look at stl. John

          T Offline
          T Offline
          ThatsAlok
          wrote on last edited by
          #4

          John M. Drescher wrote:

          Although I have used MFC's template classes for years in the past, I second the advice above. Take a look at stl

          Any Preculiar reason for that ?

          "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

          cheers, Alok Gupta VC Forum Q&A :- I/ IV

          J 1 Reply Last reply
          0
          • B bugDanny

            I'm trying to use CObList with a class I created. The class I created I inherited CObject. class CFAFRecord : public CObject {/*data*/} I'm able to create this and insert it into the CObList. But when I try to access the list with m_variable.GetNext(m_curPosition); and I try to assign it to a CFAFRecord object, I get the error, 'cannot convert 'class CObject' to 'class CFAFRecord'. It won't let me typecast it. It looks like it's not recognizing that CFAFRecord is a CObject. Any help here? Danny The stupidity of others amazes me!

            R Offline
            R Offline
            Rajesh match
            wrote on last edited by
            #5

            try this CFAFRecord *my_list =(CFAFRecord*) m_variable->GetNext( m_curPosition); regards Rajesh

            B R 2 Replies Last reply
            0
            • R Rajesh match

              try this CFAFRecord *my_list =(CFAFRecord*) m_variable->GetNext( m_curPosition); regards Rajesh

              B Offline
              B Offline
              bugDanny
              wrote on last edited by
              #6

              Your suggestion does not work. I've switched to using CObArray, but the same sort of thing happens. m_variable is not a pointer to the CObArray, so I don't need to dereference it using ->, I need to access it using . The problem really occurs, I guess, when I try to access one of the variables of my_list after this statement, using my_list->variable. Danny The stupidity of others amazes me!

              1 Reply Last reply
              0
              • P PJ Arends

                First bit of advice is to drop CObList and use STL's std::list instead. If you can not do that and have compeling reasons to use CObList maybe you could post some code showing how you are trying to cast the returned pointer.


                "You're obviously a superstar." - Christian Graus about me - 12 Feb '03 "Obviously ???  You're definitely a superstar!!!" - mYkel - 21 Jun '04 "There's not enough blatant self-congratulatory backslapping in the world today..." - HumblePie - 21 Jun '05 Within you lies the power for good - Use it!

                B Offline
                B Offline
                bugDanny
                wrote on last edited by
                #7

                Thanks for the advice, but I'd really like to stick with MFC in this case. I switched to using CObArray, but something very similar happens. I've found I can do CFAFRecord * my_record = (CFAFRecord *)my_variable.GetAt(index); but on the next line I try, my_record->member_variable; and I get an access violation. Danny The stupidity of others amazes me!

                P 1 Reply Last reply
                0
                • B bugDanny

                  Thanks for the advice, but I'd really like to stick with MFC in this case. I switched to using CObArray, but something very similar happens. I've found I can do CFAFRecord * my_record = (CFAFRecord *)my_variable.GetAt(index); but on the next line I try, my_record->member_variable; and I get an access violation. Danny The stupidity of others amazes me!

                  P Offline
                  P Offline
                  PJ Arends
                  wrote on last edited by
                  #8

                  You are obviously doing something wrong with code you have not posted here so far. I slapped together a quick MFC console app to show you what I did to make it work

                  /////////////////////////////////////////////////////////////////////////////
                  // The one and only application object

                  CWinApp theApp;

                  using namespace std;

                  class CMyObject : public CObject
                  {
                  public:
                  CMyObject() { i = 0; d = 0.0; }
                  int i;
                  double d;
                  };

                  CObList ObList;

                  void DumpList()
                  {
                  if (ObList.GetCount() == 0)
                  return;

                  POSITION pos = ObList.GetHeadPosition();
                  while(pos)
                  {
                      CMyObject \*p = (CMyObject \*)ObList.GetNext(pos);
                      cout << p->i << "  " << p->d << endl;
                  }
                  

                  }

                  int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
                  {
                  int nRetCode = 0;

                  // initialize MFC and print and error on failure
                  if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
                  {
                  	// TODO: change error code to suit your needs
                  	cerr << \_T("Fatal Error: MFC initialization failed") << endl;
                  	nRetCode = 1;
                  }
                  else
                  {
                  	// TODO: code your application's behavior here.
                  	CString strHello;
                  	strHello.LoadString(IDS\_HELLO);
                  	cout << (LPCTSTR)strHello << endl;
                  }
                  
                  // Fill the CObList with some data
                  for (int x = 1; x < 10; ++x)
                  {
                      CMyObject \*p = new CMyObject;
                      p->i = x;
                      p->d = x + x / 10.0;
                  
                      ObList.AddTail(p);
                  }
                  
                  DumpList();
                  
                  return nRetCode;
                  

                  }

                  Not perfect, leaks memory, but it should give you an idea.


                  "You're obviously a superstar." - Christian Graus about me - 12 Feb '03 "Obviously ???  You're definitely a superstar!!!" - mYkel - 21 Jun '04 "There's not enough blatant self-congratulatory backslapping in the world today..." - HumblePie - 21 Jun '05 Within you lies the power for good - Use it!

                  1 Reply Last reply
                  0
                  • T ThatsAlok

                    John M. Drescher wrote:

                    Although I have used MFC's template classes for years in the past, I second the advice above. Take a look at stl

                    Any Preculiar reason for that ?

                    "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

                    cheers, Alok Gupta VC Forum Q&A :- I/ IV

                    J Offline
                    J Offline
                    John M Drescher
                    wrote on last edited by
                    #9

                    Two main reasons. Power and Portability. stl offers you signifigantly more power than the MFC template classes. The ability to use algorithms on your collection classes is a very nice feature. http://oopweb.com/CPP/Documents/STL/VolumeFrames.html?/CPP/Documents/STL/Volume/prw432.htm[^] There are also way more different template (hash_maps, stacks, queues, sets ...) types than are offered with the MFCTemplates. Changing from one container type to a second usually does not require many changes at all as the iterators code is all the same. Inserts and sorting may vary though. And portability, besides being able to use stl code on different operating systems you can also use it with your win32 or atl projects without any changes to the stl code. John

                    1 Reply Last reply
                    0
                    • R Rajesh match

                      try this CFAFRecord *my_list =(CFAFRecord*) m_variable->GetNext( m_curPosition); regards Rajesh

                      R Offline
                      R Offline
                      Rajesh match
                      wrote on last edited by
                      #10

                      show ur code what u exactly doing?

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups