Jump to content
  • Advertisement
Sign in to follow this  
Adehm

C++ class variables

This topic is 574 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm new to C++ and a bit confused on how to use public variables.

 

I create a class:

class board{
    public:
    int n = 0;
};

Then I manipulate it in main:

int main() {
    board xy;
    xy.n = 10;
    cout << anotherFunction();
    return 0;
}

It would seem that only in main does n = 10; What I want to do is update it in the class so another function can use it. Is that possible? And if so how can I accomplish that?

int anotherFunction(){
    board xy;
    return xy.n; //need it to equal 10 now not 0 sense main updated it 
}

Hope this makes sense. Thanks.

Edited by Adehm

Share this post


Link to post
Share on other sites
Advertisement

Is there a way I can have a place that holds variables that all my functions can share and change?

Edited by Adehm

Share this post


Link to post
Share on other sites

Is there a way I can have a place that holds variables that all my functions can share and change?

Have a look at the singleton design pattern.

Edited by Orangeatang

Share this post


Link to post
Share on other sites

I can do it with global variables.

namespace ObjectGlobalVars {
   //Put all of your global variables here
     int n = 0;
}
using namespace std;
int anotherFunction(){
return ObjectGlobalVars::n;
}
int main() {
ObjectGlobalVars::n = 10;
cout << anotherFunction();
return 0;
}
Edited by Adehm

Share this post


Link to post
Share on other sites

Problem with pointers or references as I understand it; is passing a multi dimensional variable. From what I understand is I can't pass that all through one variable, though I could be wrong. I was working on this: http://www.hackerrank.com/challenges/battleship1p and just wanted to be able to call functions to return a bool based on other data. Then instead of returning two pieces of data, which I don't know how to do, I could just set global variables from that function. Here is what I did to solve this problem; though I got a bad score because I don't know a better way of searching the board besides sequential until I find a target.

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
namespace board {
   //Put all of your global variables here
     int n = 0;
    int xy[10][10]; //integer data repersenting board 0 = miss or dead/1 = undiscovered/2 = hit 
    int hX = 0;
    int hY = 0;
}
using namespace std;
//-------------------------------------------------------------------------------------------------------
bool hit(int x, int y){
            //search for another hit/ y-1,y+1,x-1,x+1
    if(y - 1 > -1 && board::xy[x][y-1] == 2){ //found
            board::hX = x;
            board::hY = y-1;
            return true;
    }else if(y + 1 < board::n && board::xy[x][y+1] == 2){ //found
            board::hX = x;
            board::hY = y+1;
            return true;
    }else if(x - 1 > -1 && board::xy[x-1][y] == 2){ //found
            board::hX = x-1;
            board::hY = y;
            return true;
    }else if(x + 1 < board::n && board::xy[x+1][y] == 2){ //found
            board::hX = x+1;
            board::hY = y;
            return true;
    }
    return false;
}
//-------------------------------------------------------------------------------------------------------
bool attack(int x, int y){
            //search for another hit/ y-1,y+1,x-1,x+1
    if(y - 1 > -1 && board::xy[x][y-1] == 1){ //found
            board::hX = x;
            board::hY = y-1;
            return true;
    }else if(y + 1 < board::n && board::xy[x][y+1] == 1){ //found
            board::hX = x;
            board::hY = y+1;
            return true;
    }else if(x - 1 > -1 && board::xy[x-1][y] == 1){ //found
            board::hX = x-1;
            board::hY = y;
            return true;
    }else if(x + 1 < board::n && board::xy[x+1][y] == 1){ //found
            board::hX = x+1;
            board::hY = y;
            return true;
    }
    return false;
}
//-------------------------------------------------------------------------------------------------------
int main() {
    cin >> board::n;  
    string col;
    int hX = -1;
    int hY = -1;
    for(int y = 0;y<board::n;y++){
        cin >> col;
        int x  = 0;
        for(char& c : col) {
            switch(c){    
                case 'm' || 'd':
                    board::xy[x][y] = 0;
                    break;
                case '-':
                    board::xy[x][y] = 1;
                    break;
                case 'h':
                    board::xy[x][y] = 2;
                    //store hit
                    hX = x;
                    hY = y;
                    break;
            }
            x=x+1;
        }
    }
    //board now stored in xy
    //if hX a positive number then search around it
    if(hX > -1){
        //search for another hit
        if(hit(hX,hY)){ //another hit found
            int dirX = 0;
            int dirY = 0;
            //direction known from hX - xy.hX or similier
            //find direction
            //cerr << hX << " <> " << board::hX << endl;
            if(hX > board::hX){ //x-1
                dirX = -1;
            }else if(hX < board::hX){ //x+1
                dirX = 1;
            }else if(hY < board::hY){ //y-1
                dirY = -1;    
            }else if(hY > board::hY){ //y+1
                dirY = 1;   
            }
            //check direction until = 1
            for(int i = 1;i<5;i++){
                if(board::xy[hX+dirX*i][hY+dirY*i] == 1){ //found next attack
                    hX = hX+dirX*i;
                    hY = hY+dirY*i;
                    break;                
                } 
            }
            
        }else{ //try around it until hit
            if(attack(hX,hY)){
                hX = board::hX;
                hY = board::hY;
            }
        }      
    }else{ //find a 1 to attack
        for(int y = 0;y<board::n;y++){
            for(int x = 0;x<board::n;x++){
                if(board::xy[x][y] == 1){ //attack
                    cout << y << " " << x;
                    return 0;
                }
            }
        }
    }
    cout << hY << " " << hX;
    return 0;
}

Share this post


Link to post
Share on other sites

Scope- A block that holds a "context" of variables and code.

{

// First scope

{

 // Second scope

}

 

// Second cope is gone, returning to first scope.

}

Secondly, The two objects in main and in your functions refer to other ISNTANCES of the class board.

board xy; //is an instance of board.

You can refer to the same instance by using a reference or a pointer (which gives you the original location of your instance).

 

int func(board* xy)

{ xy->n=10;}

board xy;

func(&xy); // You pass the LOCATION of xy and not the instance. It copies the memory address instead of the value itself. 

if(xy.n==10)

{ cout << "I did it"<<endl; } 

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!