Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » Extra libraries, Code snippets, applications etc. » C++ language problems and code snippets » How to access other classes variables?
Re: How to access other classes variables? [message #9632 is a reply to message #9630] Tue, 22 May 2007 23:12 Go to previous messageGo to previous message
forlano is currently offline  forlano
Messages: 1185
Registered: March 2006
Location: Italy
Senior Contributor
michael wrote on Tue, 22 May 2007 21:13

I have a very basic problem with OOP. I have two classes and i want to access a variable in one class which is defined in the other class.

header.h
class1
{
  String a_string;
};

class2
{
 ..
};


class1.cpp
#include "header.h"

class1::class1()
{
  a_string = "TEST";
}


class2.cpp
#include "header.h"

class2::class2()
{
  PromptOK(a_string);
}


How can i use the value of a_string in class2 which is defined in class1?

It's surely very basic noob stuff, but i didn' get it after reading different tutorials.

Michael


Hi,

you have to define an object of class1 somewhere, maybe with global scope,

class1 a;

and then use it, for example a.a_string in class2.

Luigi
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Constructor with parameter
Next Topic: How to use string array with functions?
Goto Forum:
  


Current Time: Wed May 08 05:02:24 CEST 2024

Total time taken to generate the page: 0.02597 seconds