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. About pointer assignment [modified]

About pointer assignment [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
c++helpquestion
5 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.
  • M Offline
    M Offline
    milestanley
    wrote on last edited by
    #1

    a.h: static ClassOne *one; a.cpp: ClassOne::one = this; //So, can I do it like this? But an error has occured: "expected constructor, destructor, or type conversion before '=' token"

    modified on Thursday, April 15, 2010 9:54 PM

    G Richard Andrew x64R E K 4 Replies Last reply
    0
    • M milestanley

      a.h: static ClassOne *one; a.cpp: ClassOne::one = this; //So, can I do it like this? But an error has occured: "expected constructor, destructor, or type conversion before '=' token"

      modified on Thursday, April 15, 2010 9:54 PM

      Richard Andrew x64R Offline
      Richard Andrew x64R Offline
      Richard Andrew x64
      wrote on last edited by
      #2

      In a.cpp, try:

      ClassOne *one = this;

      What was I thinking?

      modified on Thursday, April 15, 2010 10:44 PM

      1 Reply Last reply
      0
      • M milestanley

        a.h: static ClassOne *one; a.cpp: ClassOne::one = this; //So, can I do it like this? But an error has occured: "expected constructor, destructor, or type conversion before '=' token"

        modified on Thursday, April 15, 2010 9:54 PM

        G Offline
        G Offline
        Gwenio
        wrote on last edited by
        #3

        If I understand what you are asking, then it should be something like this: - a.h

        class ClassOne {
        //stuff
        void SetRef();
        //more stuff
        };
        static ClassOne* one;

        - a.cpp

        one(0);
        inline void ClassOne::SetRef() {one = this;}

        Your problems are: - "this" is only valid inside class member methods. - You do not need to have "ClassOne::" infront of "one" unless it is a static member of the class (it is declared within the scope of the class). If "one" is a static member, this a.cpp would be as follows:

        ClassOne * ClassOne::one(0);
        inline void ClassOne::SetRef() {ClassOne::one = this;}

        1 Reply Last reply
        0
        • M milestanley

          a.h: static ClassOne *one; a.cpp: ClassOne::one = this; //So, can I do it like this? But an error has occured: "expected constructor, destructor, or type conversion before '=' token"

          modified on Thursday, April 15, 2010 9:54 PM

          E Offline
          E Offline
          Emilio Garavaglia
          wrote on last edited by
          #4

          It seems you are confusing classes with instances (that is the same as confusing types with variables) local and global scope and linkage. Assuming the declaration you provided are at file scope (that is, not enclosed in some other braces) one is a pointer to a ClassOne object. It exist at global level (the pointer!) and have only local visibility (local in respect to the cpp file that "sees" it, that are all the cpp files that include the h file - in other words, static has very few sense in h files) ClassOne::one does not exist, since one is global, and not a part of ClassOne (hence the compiler doesn't know what to do), and than, this is meaningless since you are not inside a member function body. At this point we cannot correct, since we cannot figure out what you where trying to do.

          2 bugs found. > recompile ... 65534 bugs found. :doh:

          1 Reply Last reply
          0
          • M milestanley

            a.h: static ClassOne *one; a.cpp: ClassOne::one = this; //So, can I do it like this? But an error has occured: "expected constructor, destructor, or type conversion before '=' token"

            modified on Thursday, April 15, 2010 9:54 PM

            K Offline
            K Offline
            KarstenK
            wrote on last edited by
            #5

            if you need a global object create one. If not you dont need a global pointer.

            Press F1 for help or google it. Greetings from Germany

            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