c++ - Can I call a function of other unrelated class -


i not sure if allowed want give try. have 2 classes below unrelated , want call function b_method() of class b in class , interested boolean value returns.

class { public: bool a_method() { b *obj = new b();        bool varbool= obj->b_method(); return varbool; }       }  class b { public: bool b_method() { "does something" return varbool; } } 

i tried call b_method() in class other option replicate code of b_method() in a_method(), got following compiler errors.

: error c2065:'a' : undeclared identifier : error c2065: 'obj' : undeclared identifier : error c2061: syntax error : identifier 'a' : error c2228: left of '->b_method' must have class/struct/union         type ''unknown-type'' 

you need define method before call it.

also, if method static (you can mark static if doesn't use of b's instance variables), can call b::b_method() anywhere , run. no need make instance.

finally, don't forget delete instances! or use

b obj; bool varbool= obj.b_method(); return varbool; 

instead (which doesn't allocate dynamic memory, no need delete)


Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -