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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Simple questions for the experts

Simple questions for the experts

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestionc++tutorial
5 Posts 3 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.
  • N Offline
    N Offline
    NewHSKid
    wrote on last edited by
    #1

    Hi everyone, I wanted to know if someone could help me. I have a project with three different classes for three different dialogs(3 .cpp files). I have a variable that the user enters, which is in one of the classes (dialogs). I want to use that same variable with the stored information in my other dialog(class). How do I do that? Someone told me to pass a pointer, but i don't know how to exactly do that cause i get some error when i run the program. Thanks for helping me. Jimmy Just cause I am 15, doesn't mean I'm dumb! (I'll really be 4 on Feb. 29...the year 2004)

    B B 2 Replies Last reply
    0
    • N NewHSKid

      Hi everyone, I wanted to know if someone could help me. I have a project with three different classes for three different dialogs(3 .cpp files). I have a variable that the user enters, which is in one of the classes (dialogs). I want to use that same variable with the stored information in my other dialog(class). How do I do that? Someone told me to pass a pointer, but i don't know how to exactly do that cause i get some error when i run the program. Thanks for helping me. Jimmy Just cause I am 15, doesn't mean I'm dumb! (I'll really be 4 on Feb. 29...the year 2004)

      B Offline
      B Offline
      Babayan Hovhannes
      wrote on last edited by
      #2
      1. You can declare your variable in global scope. 2) You can use pointer to the variable in the first class in the last two classes. yiy
      N 1 Reply Last reply
      0
      • B Babayan Hovhannes
        1. You can declare your variable in global scope. 2) You can use pointer to the variable in the first class in the last two classes. yiy
        N Offline
        N Offline
        NewHSKid
        wrote on last edited by
        #3

        How would I use a pointer to the variable in the class so that the other classes could use it? Can you help me with this. I am new to programming, and am trying to teach myself with a book. (i'm only 15) Jimmy Just cause I am 15, doesn't mean I'm dumb! (I'll really be 4 on Feb. 29...the year 2004)

        B 1 Reply Last reply
        0
        • N NewHSKid

          How would I use a pointer to the variable in the class so that the other classes could use it? Can you help me with this. I am new to programming, and am trying to teach myself with a book. (i'm only 15) Jimmy Just cause I am 15, doesn't mean I'm dumb! (I'll really be 4 on Feb. 29...the year 2004)

          B Offline
          B Offline
          Babayan Hovhannes
          wrote on last edited by
          #4

          I think, that the better way to do this is declare m_var as static variable. After that you can use this variable as CL1::m_var. ------------------------------------------------------------ class CL1 { public: CL1(); //constructor virtual ~CL1(); //destructor public: static int m_var; //your variable public: void SetVariable(int value) }; CL1::m_var=0; //linking will fail without this. CL1::Cl1() { } CL1::~CL1() { } void CL1::SetVariable(int value) { m_var=value; } #include using namespace std; int main() { CL1 myclass; int user_val; cin>>user_val; myclass.SetVariable(user_val); cout<<"After input "<

          1 Reply Last reply
          0
          • N NewHSKid

            Hi everyone, I wanted to know if someone could help me. I have a project with three different classes for three different dialogs(3 .cpp files). I have a variable that the user enters, which is in one of the classes (dialogs). I want to use that same variable with the stored information in my other dialog(class). How do I do that? Someone told me to pass a pointer, but i don't know how to exactly do that cause i get some error when i run the program. Thanks for helping me. Jimmy Just cause I am 15, doesn't mean I'm dumb! (I'll really be 4 on Feb. 29...the year 2004)

            B Offline
            B Offline
            Bob Stanneveld
            wrote on last edited by
            #5

            Hello, the following should work: In the dialog class add the following:

            private:
            VarType m_Var;
            public:
            VarType GetVar() {return m_Var;}

            In the other classes at a point where you want to use the var add

            // can only appear in functions and constructors
            VarType var = YourClass.GetVar();

            this should do the trick! Good luck! :)

            A student knows little about a lot. A professor knows a lot about little. I know everything about nothing.

            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