returning vector from a class member function

Pages: 12
@keskiverto I think bold statement is doing nothing until you return something.
@thmm I'll do it in the near future
resabzr wrote:
@keskiverto I think bold statement is doing nothing until you return something.

Perhaps you ought to look more carefully at the definition of Example?
@keskiverto

if answer is public it gives 42.
but if it is private I don't have access to that. So how this void function returning a value?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<iostream>

class Example {
public:
	int answer;

	void memfun();
};

void Example::memfun() {
	answer = 42;
};
int main()
{
	Example one;
	one.memfun();
	int Answer=one.answer;
	std::cout << Answer << std::endl;
}
Last edited on
So how this void function returning a value?

It isn't returning anything.
It's setting the member variable to 42 and nothing else.
Yes,it setting the member variable.
@resabzr: You did ask:
I have a class member function which is a void function and calculates one of my class variables.
Is there any way to access this class variable?
1
2
3
4
5
6
7
8
class Example {
  int answer;
  void memfun();
};

void Example::memfun() {
  answer = 42;
};

The memfun() is "a class member function which is a void function".
The answer is a "class variable".

The memfun() does "access" the class variable answer.
The memfun() does modify the class variable answer.

The thing that the "void function" does "calculate" (42) is stored into class variable by the function.

This statement answer = 42; is an assignment. It assigns a value to variable.


That is what you did ask. How your "void function" should access a member variable.
That is what your function did (for Nrow_ptr_), until you did change the function.
That is what your function did not do for row_ptr_.


You have "a big code". You don't seem to understand any of it, yet you keep adding more. That cannot succeed.
You don't seem to understand what we say, ask, or tell.

You should study the basics of C++ again. For example, start with http://www.cplusplus.com/doc/tutorial/


I'm done.
@keskiverto first of all I am really grateful for your time and explanations. I really do appreciate that. Please don't interpret my answer as rude But I think there is some misunderstanding here. Maybe I asked my question not in the best way I could, by the way I'm not a software engineer. Otherwise I would be posting answers not questions. I just started object oriented programming around one month ago, and all the code I have I wrote it by myself and I understand what I have written so far. Actually I wrote it before in c and now I'm moving to c++. In the previous comments and answers posted here I was searching for a way to use the class variable set in the void member function outside the void function. And I find a solution just by passing the desired variable by refrence as an argument in the void function. Another way to say, imagine in this void memfun we are setting a vector of vector of double and I need this in other part of code. Anyway I apologize if I've caused you any inconvenience by my question and again thank you for your answers.
Last edited on
resabzr wrote:
but if it is private I don't have access to that.

This is not true. Example::memfun() is a member of the Example class, so it has access to all of the class's private members.

If you want code outside the Example class to have access to the value of Example::answer, you can write an accessor method, e.g.

1
2
3
4
int Example::getAnswer() const
{
  return answer;
}


(Hopefully we can avoid repeating the same tedious discussion about accessor methods that we've had a thousand times before.)
Last edited on
Topic archived. No new replies allowed.
Pages: 12