function call without creating object

E

Eric Kaplan

in C++ you can call function / methods without creating an object of
that Class?

like the the following, it calls Update() + Draw() functions by Class
name CInterfaceGroup + CContainer.

In Java, one can only call method by Class name if it is static
method.

In C++, you can call any function without creating an Object like -
CContainer * cc = new CContainer;
cc->Draw();

=========================

CGraphicsGroup::~CGraphicsGroup() {
}

void CGraphicsGroup::Create() {

}

void CGraphicsGroup::Update() {
CInterfaceGroup::Update();
}

void CGraphicsGroup::Draw() {
CContainer::Draw();
}
 
H

hurcan solter

in C++ you can call function / methods without creating an object of
that Class?

like the the following, it calls Update() + Draw() functions by Class
name CInterfaceGroup + CContainer.

In Java, one can only call method by Class name if it is static
method.

In C++, you can call any function without creating an Object like -
CContainer * cc = new CContainer;
cc->Draw();

=========================

CGraphicsGroup::~CGraphicsGroup() {

}

void CGraphicsGroup::Create() {

}

void CGraphicsGroup::Update() {
CInterfaceGroup::Update();

}

void CGraphicsGroup::Draw() {
CContainer::Draw();

}

No you can't call a member function without a object (at least the way
you do)
unless it's static.

And what was your question?
 
C

Christian Hackl

Eric said:
in C++ you can call function / methods without creating an object of
that Class?

[...]

In Java, one can only call method by Class name if it is static
method.

Just like in C++. However, notice the different call syntax:

class Example
{
public:
static void f();
};

Example.f(); // won't compile
Example::f(); // correct syntax
In C++, you can call any function without creating an Object like -
CContainer * cc = new CContainer;
cc->Draw();

This code *does* create an object.
 
J

Joe Greer

in C++ you can call function / methods without creating an object of
that Class?

Yes, no, maybe. Hard to tell what you are actually after, so I will
give the long form. In C++ you can call free functions, static methods
without creating an instance of a class. For example:

int f()
{
return 5;
}

class A
{
public:
static int f1()
{ return 6;
int f2() { return 7;}
};

int main()
{
f(); // OK
A::f1(); // OK
A::f2(); // Bzzzt. Not OK
A a; // Create instance of A on stack
a.f2(); // OK
}
like the the following, it calls Update() + Draw() functions by Class
name CInterfaceGroup + CContainer.

In Java, one can only call method by Class name if it is static
method.

In general, expecting things to be like Java can get you into trouble,
but in this case things are pretty much the same.
In C++, you can call any function without creating an Object like -
CContainer * cc = new CContainer;
cc->Draw();

I fail to see how this is an example of not creating an object... And
this statement is false.

joe
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
474,176
Messages
2,570,947
Members
47,501
Latest member
Ledmyplace

Latest Threads

Top