Link to home
Start Free TrialLog in
Avatar of forums_mp
forums_mp

asked on

console access issue


# include <iostream>

class foo {
  int param1;
  int param2;
  int param3;
  void incement();
public:
   foo();
   void print();
};

/// cpp
foo foo_exec;                                                        // (1)
foo::foo() : param1(0), param2(0), param3(0)  {}

void foo::increment() {
     ++param1;
     ++param2;    
     ++param3;
}

void foo::print() {
  cout << param1 << endl;    
  cout << param2 << endl;    
  cout << param3 << endl;    
}

extern 'c' foo_print() {                                        // (2)
  foo_exec.print();
}

For console access purposes, - in the past what I'd do is.  Create an instance of 'foo' (see item (1)) and an extern function (see item (2)).  This way at the console (a development environtment issue.  can't call c++ functions directly) I could type "foo_print" and all's well.  

I'd like to refrain from doing item 1 and 2.  So I thought:

static void foo_print() {
   cout << foo::param1 << endl;    // param1 is private but needs to be.
   // etc
}

Doesn't work since param1 is private..

Realistically I'd like to have " friend ostream& operator<< (ostream& output_, foo& foo_); ".   The call said function from foo_print().  How do I achieve that?  Source example please!!
SOLUTION
Avatar of Axter
Axter
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Notice I made the input foo argument constant for the operator.
It's good practice when ever possible to make arguments constant.
Avatar of forums_mp
forums_mp

ASKER

Perhaps, I messed up in my initial post.  I understand the operator << part.    What I need is a static function or equivalent that I can TYPE from the console.    for instance

static void print_foo()
{

}

I coudl type print_foo and print function should then call "operator<< (ostream& output_, const foo& foo_)".   But I DONT want to have to instantiate item 1 for this to work.   Is that possible?

IOW: in the past i've done this

extern 'c' foo_print() {                                        // (2)
  foo_exec.print();
}

From the console I could type foot_print and I get couts on the params within the member function 'print()'.  Troubel is I have to instantiate the class via (1).   I dont want to do that.

It appears though that EVEN if I could do

extern 'c' foo_print() {                                        // (2)
  cout << foo;
}
Then I still have to intantiate foo via (1).   I'm trying to avoid the instantiation within the cpp at all cost.
In order to use the static function you either need to have an instance of foo, or you need to use all static data members.
Many questions:

How could foo_print print out "foo" without a declaration for foo?   If foo_print is in an external module, how does IT get access to foo?

How can you type foo_print at the console and get function foo_print() to be called?

very confused here.

Axter, you gave me an idea.  So now consider:

// foo.h
 # include<iostream>
 # include<algorithm>
using namespace std;

class foo {
  int idx;
  int jdx;
public:
  foo();
  void increment ();
  friend ostream& operator<< (ostream& output_, const foo& foo_);
};

/////// foo.cpp
# include "foo.h"
foo::foo() : idx(0), jdx(0) {}

void foo::increment () {
  ++idx, ++jdx;
}

ostream& operator<< (ostream& output_, const foo& foo_)
{
  cout << foo_.idx << endl;    
  cout << foo_.jdx << endl;    
  return output_;
}

/////// main.cpp
foo *foo_(0);
int main()
{
  foo_ = new foo();
  foo_->increment();
  delete foo_;
}

static void print()    /// console access to print foo
{
  if (foo_)
    cout << foo_ << endl;
}
////// end main.cpp

You see what I was wrestiling with was how do I move the 'print' function within foo.cpp with this (pointer to foo) approach.  it seems though that this will be as good as it gets?  Right!!
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

I see.  Good point.  Forgot about the singleton.  

Now you mentioned: 'several different'... name others!!
>>Now you mentioned: 'several different'... name others!!

The other methods are baiscally different variations of the Singleton method.