I tried searching for this but didnt get any results. What I would like to do is have "function a" use "variable b" which is stored in "function c". This shouldnt be terribly complicated, but I am having trouble devising a way to do this.
I tried searching for this but didnt get any results. What I would like to do is have "function a" use "variable b" which is stored in "function c". This shouldnt be terribly complicated, but I am having trouble devising a way to do this.
Pass as a parameter, or return it as a result.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
>What I would like to do is have "function a" use "variable b" which is stored in "function c".
If "function a" needs "variable b" then why is it stored in "function c"? Wouldn't it make more sense for both function a and c to be member functions in a class where variable b is a data member? It sounds to me like your design needs a refit rather than a kludge.
My best code is written with the delete key.
either have function a return variable b, or have variable b passed to function c as a reference so that any change made in function a would reflect in function c. Of course varialbe b would have to be defined in the global scope of both functions.
or it could all be one class, where variable B is a private/public member that both function a and c could change
Oh classes, I figure I would have to use them sooner or later, I will give a crack at it and if I have any more question I shall post my code. Thankyou.
or a struct.
Alright, when you declare a public variable (like int a), how do you use that variable in seperate function? Also how might a struct work with this?
by the way, when I try, I get the error: 12 new types may not be defined in a return type
Read the tutorials on this website about functions lilrayray... pretty please
Originally Posted by brewbuck:
Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.
I beleive I did and they didnt seem to conver more then their basic use and how to declare them; that is unless i am missing somethign?
You saw this? http://www.cprogramming.com/tutorial/lesson4.html
I think this means that you have as the return type of a function "class foo", and class foo has not been declared.error: new types may not be defined in a return type
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
I am hiving trouble with classes. Once you declared and have written a function that is a member of a class, how do you use that function with the main function? for example:
Also, what are constructors and destructors? do I need them?Code:#include <cstdlib> #include <iostream> using namespace std; class t{ public: void test(); int a; }; int main(void) { test(); cin.get(); } void t::test(){ a = 10; cout<<a; }
Hint: static variables/functions.
A static variable is a variable that exists only once for every instance of the class; a static method can access such static variables.
orCode:class c { private: static int x; public: int get_x() { return x; } void set_x(int t) { x = t; } }; void function() { c::set_x(5); cout << c::get_x(); }
You could use a structure instead of a class for the second example, which would allow you to drop the "public:".Code:class c { public: static int x; }; void function() { c::x = 5; cout << c::x; }
[edit]
Constructors and destructors are special methods called every time an instance of the inclosing class is created or destoyed, respectively. You don't need to use them for a class that only has static variables.
[/edit]
[edit=2]
To give an initial value to a static variable, try this:
[/edit]Code:class c { public: static int x; }; int c::x = 5;
Last edited by dwks; 08-24-2006 at 03:29 PM.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
i take it you just prefer to ask us intead of reading the tutorials right?
"What the heck! Why should I do all the work. Let someone answer my doubts while I sit here calmly waiting. Searching is for those nerds who like to know everything"
A member function is used with the help of the member access operator.
Constructors are ran when you instantiate an object of your class. They are usually used to initialize data membrs and do some administrative tasks.Code:t myObject; myObject.test(); // <-- this is how you use it
Destructors are ran when the object of the class dies (goes out of scope or is explicitely deleted). Usually they are used to do some cleaning up.
Originally Posted by brewbuck:
Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.
For something as simple as a variable that can be accessed from two functions, you probably don't need a class. As mentioned, pass arguments:
Or return values:Code:void print(int x) { cout << x; }
Or pass references to see your changes:Code:void get() { int x; cin >> x; return x; }
Output:Code:#include <iostream> using std::cout; using std::endl; void add(int x) { x ++; } void addref(int &x) { x ++; } int main(void) { int x = 5; cout << x << endl; add(x); cout << x << endl; addref(x); cout << x << endl; return 0; }
As you can see, if you pass a regular variable, the function gets a local copy of it and can do what it wants to it. It you want the changes to be reflected, return the variable after you've modified it, or pass a reference/pointer.Code:5 5 6
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.