Static member function

T

thomas.heggelund

If I have a class like this one:

class test {

private:
int test_integer1;
int test_integer2;
public:

test(int first, int second) {
this->test_integer1 = first;
this->test_integer2 = second;
}

static int test_function(test * ptr);
}

If I call test::test_function with a pointer to a test class, would the
function be able to access test_integer1 and test_integer2??

let's say that test_function is something like this:
int test::test_function(test * ptr) {
return ptr->test_interger1 + ptr_test_integer2;
}

Is this possible?
 
M

Mark P

If I have a class like this one:

class test {

private:
int test_integer1;
int test_integer2;
public:

test(int first, int second) {
this->test_integer1 = first;
this->test_integer2 = second;
}

static int test_function(test * ptr);
}

If I call test::test_function with a pointer to a test class, would the
function be able to access test_integer1 and test_integer2??

let's say that test_function is something like this:
int test::test_function(test * ptr) {
return ptr->test_interger1 + ptr_test_integer2;
}

Is this possible?

Yes, why would you think otherwise?
 
I

Ian Collins

If I have a class like this one:

class test {

private:
int test_integer1;
int test_integer2;
public:

test(int first, int second) {
this->test_integer1 = first;
this->test_integer2 = second;
}

Try using initialiser lists:

test(int first, int second)
: test_integer1(first), test_integer2(second) {}
static int test_function(test * ptr);
}

If I call test::test_function with a pointer to a test class, would the
function be able to access test_integer1 and test_integer2??

let's say that test_function is something like this:
int test::test_function(test * ptr) {
return ptr->test_interger1 + ptr_test_integer2;
}

Is this possible?
Yes, the same access rules apply to static and normal member functions.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top