LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-05-2006, 11:30 PM   #1
harrylee2003
LQ Newbie
 
Registered: Nov 2004
Posts: 23

Rep: Reputation: 15
Question for calling function


if i have a class, how can i use the data in the same class function. for example..

class sameclass
{public:....
....
private:
a[];};
void sameclass::example(int n)
{
while(n!=0){
cin>>n;
a[i];
i++;}
}
void sameclass::sortTwo(sameclass n1, smaeclass n2)
{
//how can i use the data in the example function
//when i use sameclass s3 = s1.a[]; it said i can't use the pricate member..
}

Last edited by harrylee2003; 08-05-2006 at 11:32 PM.
 
Old 08-06-2006, 02:18 AM   #2
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Private members should be accessible from member functions of the same class. The following example works fine:

Code:
class classA
{
  public:
   void setMember(int m) { member = m; }
   void someFunction(classA a);
  private:
   int member;
};

void classA::someFunction(classA a)
{
  classA c;
  c.setMember(a.member) // Access private member of same class
  cout << c.member << endl;
}

int main()
{
  classA a1;
  a1.setMember(3);

  classA a2;
  a2.someFunction(a1);

  return 0;
}
I'm having problems understanding your code though, for example, you've declared an array with no type (a[]), you haven't declared i anywhere and then you want to use "sameclass s3 = s1.a[];", but if you want to assign an element of that array to s3 then you need to use an index. Also, s1 doesn't appear to exist - in your function, you've passed objects of type sameclass called n1 and n2. If you could show us your code in its entirety and the exact error messages, that would help.

Also, you might want to use code tags, as they'll preserve your indentation if you're copying and pasting from a text editor.

As an aside, if you were trying to access a private member from another class, you would have to write a public accessor function, e.g.

Code:
class classA
{
   public:
     int getMember() { return member; }
   private:
      int member;
};
Code:
void classB::someFunc(classA a)
{
   int x = a.getMember();
   // Do stuff with x
}

Last edited by Nylex; 08-06-2006 at 03:18 AM.
 
Old 08-06-2006, 04:09 AM   #3
harrylee2003
LQ Newbie
 
Registered: Nov 2004
Posts: 23

Original Poster
Rep: Reputation: 15
so if i need to get the private data, i need to use function?
like
ClassA s1,s2,s3
s1 and s2 is going to set some num into private data member
and s3 use in other function in same class to mix s1 and s2
so i need to write a get function to able to get private date in s1 and s2?
am i right?
thank
 
Old 08-06-2006, 07:04 AM   #4
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
If all the objects are of the same class and the members you want to access are private then you don't need to use an accessor function - see the first example I gave. Let me explain a bit about what it's doing:

In main(), I create two objects of classA (a1 and a2). I then set the value of a1's member variable, member using the setMember(int) function. Obviously I can't set it directly (i.e. with "a1.member = 3;") because member is private.

Next, I call classA's member function, someFunction(classA a) on my object a2 and pass a1 as an argument. That function is again:

Code:
void classA::someFunction(classA a)
{
  classA c;
  c.setMember(a.member);
  cout << c.member << endl;
}
What I'm doing here is to create another object and then set the value of its private member (i.e. member) to the same value as a's. c and a are both of type classA, so in this function I can access a's member directly (i.e. "a.member") without an accessor function ("a.getMember()").

Again, it's only if you're trying to access a private member from a different class (or main or some other function not in a class) that you'll need to use an accessor function (which of course will need to be public so that it can be used).

HTH.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
calling a function from a pointer spx2 Programming 3 05-25-2006 04:52 PM
calling function in kernel vishalbutte Programming 0 02-14-2006 07:34 AM
Calling another function from a function using GTK geminigal Programming 4 07-11-2005 03:15 PM
switch statements calling a function tekmorph Programming 2 10-19-2004 05:53 PM
Calling my function through MACROS in OpenOfficeWriter mcp_achindra Linux - Software 0 07-05-2004 06:56 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:07 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration