• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 220
  • Last Modified:

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!!
0
forums_mp
Asked:
forums_mp
  • 5
  • 3
2 Solutions
 
AxterCommented:
Here's some example code:

class foo {
  int param1;
  int param2;
  int param3;
public:
   void increment();
  foo();
   void print();
   friend ostream& operator<< (ostream& output_, const foo& foo_);
};

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

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

void foo::print() {
}

ostream& operator<< (ostream& output_, const foo& foo_)
{
  cout << foo_.param1 << endl;    
  cout << foo_.param2 << endl;    
  cout << foo_.param3 << endl;    
      return output_;
}

int main(int argc, char* argv[])
{
      foo myfoo;
      myfoo.increment();

      cout << myfoo << endl;

      system("pause");
      return 0;
}
0
 
AxterCommented:
Notice I made the input foo argument constant for the operator.
It's good practice when ever possible to make arguments constant.
0
 
forums_mpAuthor Commented:
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.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
AxterCommented:
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.
0
 
grg99Commented:
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.
0
 
forums_mpAuthor Commented:

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!!
0
 
AxterCommented:
>>it seems though that this will be as good as it gets?  Right!!

There are several different methods you can use for this, but which method is better depends on your total requirement.

For example, if you only need to create on instance of foo, then you could make foo a singleton class, and have print call the Singleton::GetInstance static function.
0
 
forums_mpAuthor Commented:

I see.  Good point.  Forgot about the singleton.  

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

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

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now