Static member function

Discussion in 'C++' started by thomas.heggelund@gmail.com, Jan 13, 2007.

  1. Guest

    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?
    , Jan 13, 2007
    #1
    1. Advertising

  2. Mark P Guest

    wrote:
    > 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?
    Mark P, Jan 13, 2007
    #2
    1. Advertising

  3. Ian Collins Guest

    wrote:
    > 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.

    --
    Ian Collins.
    Ian Collins, Jan 13, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Victor Bazarov
    Replies:
    0
    Views:
    1,348
    Victor Bazarov
    Jun 23, 2003
  2. 0to60
    Replies:
    4
    Views:
    502
    jeffc
    Nov 21, 2003
  3. aling
    Replies:
    6
    Views:
    466
    Xiaobin.Huang
    Oct 30, 2005
  4. dolphin
    Replies:
    3
    Views:
    1,324
    Pete Becker
    Dec 5, 2007
  5. paul
    Replies:
    8
    Views:
    698
    Alf P. Steinbach
    Apr 30, 2009
Loading...

Share This Page