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. MFC classes in my class

MFC classes in my class

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++graphicshelp
17 Posts 6 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.
  • P pie

    Hello. When I write something like: --- class test{ public: CBrush f; }; int main() { test t; vector hej; hej.push_back(t); } --- I get an error in : ...\include\vector(575): error C2440: 'initializing' : cannot convert from 'const test' to 'test' But if I replace CBrush with one of my own classes, i everything works fine! What is it with those MFC classes?

    D Offline
    D Offline
    David Crow
    wrote on last edited by
    #7

    Do you have all the necessary #includes in place?


    "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

    P 1 Reply Last reply
    0
    • P pie

      Why did you want to use a pointer in the first place? What is the advantage?

      J Offline
      J Offline
      jmkhael
      wrote on last edited by
      #8

      For various reasons: Parameter passing to a function of objects is always better by pointer or by reference, the copy time is smaller and faster. and the allocator of your vector is faster too because you wouldn't reallocate data or allocate chunks often. and much more too like copy modifications ... Papa while (TRUE) Papa.WillLove ( Bebe ) ;

      P 1 Reply Last reply
      0
      • D David Crow

        Do you have all the necessary #includes in place?


        "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

        P Offline
        P Offline
        pie
        wrote on last edited by
        #9

        I have: #include #include using namespace std;

        D 1 Reply Last reply
        0
        • J jmkhael

          For various reasons: Parameter passing to a function of objects is always better by pointer or by reference, the copy time is smaller and faster. and the allocator of your vector is faster too because you wouldn't reallocate data or allocate chunks often. and much more too like copy modifications ... Papa while (TRUE) Papa.WillLove ( Bebe ) ;

          P Offline
          P Offline
          pie
          wrote on last edited by
          #10

          But i'm not looking for speed-improvements. I just want to know why the code i posted doesn't work. MUST I use pointers to make it work ?

          J 1 Reply Last reply
          0
          • P pie

            But i'm not looking for speed-improvements. I just want to know why the code i posted doesn't work. MUST I use pointers to make it work ?

            J Offline
            J Offline
            jmkhael
            wrote on last edited by
            #11

            Using pointer will settle this for you. if you dont want to having a copy constructor will do Papa while (TRUE) Papa.WillLove ( Bebe ) ;

            1 Reply Last reply
            0
            • P pie

              Hello. When I write something like: --- class test{ public: CBrush f; }; int main() { test t; vector hej; hej.push_back(t); } --- I get an error in : ...\include\vector(575): error C2440: 'initializing' : cannot convert from 'const test' to 'test' But if I replace CBrush with one of my own classes, i everything works fine! What is it with those MFC classes?

              N Offline
              N Offline
              Navin
              wrote on last edited by
              #12

              You probably need to define a copy constructor for your class. E.g., class test { public: test(const test &src); CBrush f; }; test::test(const test &src) :f(src.f) { } Then try it and see what happens. (Note, just a guess, I didn't actually try to compile this.) Sometimes I feel like I'm a USB printer in a parallel universe.

              1 Reply Last reply
              0
              • P pie

                I have: #include #include using namespace std;

                D Offline
                D Offline
                David Crow
                wrote on last edited by
                #13

                No compiler errors with this:

                class test
                {
                public:
                CBrush f;
                test();
                test(const test& t);
                ~test();
                const test& operator=(const test& t);
                };

                void main( void )
                {
                test t;
                vector hej;

                hej.push\_back(t);
                

                }


                "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

                P 1 Reply Last reply
                0
                • D David Crow

                  No compiler errors with this:

                  class test
                  {
                  public:
                  CBrush f;
                  test();
                  test(const test& t);
                  ~test();
                  const test& operator=(const test& t);
                  };

                  void main( void )
                  {
                  test t;
                  vector hej;

                  hej.push\_back(t);
                  

                  }


                  "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

                  P Offline
                  P Offline
                  pie
                  wrote on last edited by
                  #14

                  Yeah, that seems to work.. Thank you(and Navin) :)

                  1 Reply Last reply
                  0
                  • P pie

                    Hello. When I write something like: --- class test{ public: CBrush f; }; int main() { test t; vector hej; hej.push_back(t); } --- I get an error in : ...\include\vector(575): error C2440: 'initializing' : cannot convert from 'const test' to 'test' But if I replace CBrush with one of my own classes, i everything works fine! What is it with those MFC classes?

                    P Offline
                    P Offline
                    Paul Ranson
                    wrote on last edited by
                    #15

                    Objects that can be stored in std:: containers must be copyable, and copies must be equivalent. This isn't the case with CBrush. Even if you arrange a copy constructor such that the object being copied to takes ownership,

                    test ( const test& t)
                    {
                    f.Attach ( t.f.Detach ()) ;
                    }

                    you haven't met the 'equivalent' condition since the new object owns the Windows Brush and the old one no longer does. In your example if 't' was initialised with a valid brush after the push_back would no longer own it. The copy constructor above won't compile because the argument 't' is const and we're trying to modify it with 'Detach', sound familiar? In these circumstances it is appropriate to store a container of pointers and manage the destruction of the contained pointers manually. Paul

                    P N 2 Replies Last reply
                    0
                    • P Paul Ranson

                      Objects that can be stored in std:: containers must be copyable, and copies must be equivalent. This isn't the case with CBrush. Even if you arrange a copy constructor such that the object being copied to takes ownership,

                      test ( const test& t)
                      {
                      f.Attach ( t.f.Detach ()) ;
                      }

                      you haven't met the 'equivalent' condition since the new object owns the Windows Brush and the old one no longer does. In your example if 't' was initialised with a valid brush after the push_back would no longer own it. The copy constructor above won't compile because the argument 't' is const and we're trying to modify it with 'Detach', sound familiar? In these circumstances it is appropriate to store a container of pointers and manage the destruction of the contained pointers manually. Paul

                      P Offline
                      P Offline
                      pie
                      wrote on last edited by
                      #16

                      Thank you, Paul, that was a good answer.

                      1 Reply Last reply
                      0
                      • P Paul Ranson

                        Objects that can be stored in std:: containers must be copyable, and copies must be equivalent. This isn't the case with CBrush. Even if you arrange a copy constructor such that the object being copied to takes ownership,

                        test ( const test& t)
                        {
                        f.Attach ( t.f.Detach ()) ;
                        }

                        you haven't met the 'equivalent' condition since the new object owns the Windows Brush and the old one no longer does. In your example if 't' was initialised with a valid brush after the push_back would no longer own it. The copy constructor above won't compile because the argument 't' is const and we're trying to modify it with 'Detach', sound familiar? In these circumstances it is appropriate to store a container of pointers and manage the destruction of the contained pointers manually. Paul

                        N Offline
                        N Offline
                        Nemanja Trifunovic
                        wrote on last edited by
                        #17

                        Paul Ranson wrote: In these circumstances it is appropriate to store a container of pointers and manage the destruction of the contained pointers manually. Or even better, to use some smart pointers, like boost::shared_ptr or Loki::SmartPtr

                        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