Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

RE: [VxW] constructor question

66 views
Skip to first unread message

Cathell, Daniel

unread,
Jul 6, 2004, 2:13:32 PM7/6/04
to VxWorks Users Group List
Try something like this:

class Test {
public:
Test();
run();
private:
int data;
};

Test::Test()
{
data=100;
}

Test::run()
{
cout << data << endl;
}

extern "C" {
void TestMyTestClass(void) {
Test t;
t.run();
}
}

Now simply call TestMyTestClass from the WindShell. Your constructor will be called and things should happen as you expect them.

Daniel Cathell
ALPS Software Engineer
daniel....@lmco.com
Lockheed Martin MS2
Desk: 410-682-0564
Fax: 410-682-0782


-----Original Message-----
From: vxwexplo...@csg.lbl.gov
[mailto:vxwexplo...@csg.lbl.gov]On Behalf Of David
Sent: Tuesday, July 06, 2004 1:39 PM
To: vxwe...@csg.lbl.gov
Subject: [VxW] constructor question


Can anyone explain how objects are created in VxWorks at startup?

Here's my sample scenario:

Class Test
{
public:
Test();
run();
Private:
int data;
};

Test::Test()
{
data=100;
}

Test::run()
{
cout << data << endl;
}

Now I compiled this and downloaded it to the target. In the shell I
call run(). The constructor is never called. What is the proper way
to start this program? Do I need a global function, like a main() to
instantiate the object so that the constructors are called? Is there
a way for me to create the object in the shell and then call it's run
method? I've got a feeling that I need to do this ...

e.g.

int main() // or some other static/global function name
{
Test test;
test.run();
}


Thanks in advance of any advice that you can offer.

Regards,
David
_______________________________________________
VxWorks Users Group mailing list
VxWe...@lbl.gov
http://www-csg.lbl.gov/vxworks/

David

unread,
Jul 7, 2004, 7:15:47 AM7/7/04
to
That is what I'd thought, except that if I have another function
called stop(), how do I call that method on the same object that I'd
used to call run()? When I tried this on the shell, it did not call
the method on the same object.

-David

"Cathell, Daniel" <daniel....@lmco.com> wrote in message news:<mailman.46.108913...@csg.lbl.gov>...

Frederic Brehm

unread,
Jul 7, 2004, 8:23:49 AM7/7/04
to VxWorks Users Group List
At 07:15 AM 7/7/2004, David wrote:
>When I tried this on the shell,

The shell lets you call C functions with integer arguments. You pass other
kinds of arguments (e.g., string pointers) if they fit into an integer.

To call a C++ method from the shell, you have to wrap it in a C function.

Fred


_______________________________________________________________
Frederic W. Brehm, Sarnoff Corporation, http://www.sarnoff.com/

Cathell, Daniel

unread,
Jul 7, 2004, 9:29:11 AM7/7/04
to VxWorks Users Group List
David,

I think you may have a HUGE misunderstanding of using C++ with VxWorks. You CAN NOT call class member functions from the Wind Shell and have them behave as you expect them to. The Wind Shell is almost comparable to a C interpreter. Notice I said C, not C++. You can fudge the shell to doing some C++ things by declaring C functions that call C++ methods. However to call Stop you can do something like this.

class Test {
public:
Test() {
cout << "Constructor Called" << endl;
data = 100;
}
~Test() {
cout << "Destructor Called" << endl;
}
Run() {
cout << "Run()" << data << endl;
}
Stop() {
cout << "Stop()" << data << endl;
}
private:
int data;
};

Test t;

extern "C" {
void RunMyClass(void) {
t.Run();
}

void StopMyClass(void) {
t.Stop();
}
}

Notice how I declared a Test instance, t, as a global variable. The C function RunMyClass() and StopMyClass() are callable from the shell and operate on global t. If you directly call Run or Stop, things probably get a little messy unless they are static members of the class. These C functions should be used like a test harness to unit test your class or classes. Other classes in your application should create instances of classes and manipulate them by using the member functions like any C++ program would.

David

unread,
Jul 8, 2004, 12:43:41 PM7/8/04
to
Daniel,

I did not realize that Windshell was a C interpreter, but your reply
is very clear and I now understand how to interact with C++ objects
through the interpreter.

Thanks for the insight,

Regards,
David

"Cathell, Daniel" <daniel....@lmco.com> wrote in message news:<mailman.49.108920...@csg.lbl.gov>...

0 new messages