Mombu the Programming Forum

Go Back   Mombu the Programming Forum > Programming > acces base class member from derive class
User Name
Password
REGISTER NOW! Mark Forums Read




Reply Bookmark and Share
1 18th November 15:06
gyan
External User
 
Posts: 1
Default acces base class member from derive class



Please go though following program:
#include <iostream.h>

class base {
public:
int basepublic;
protected:
int baseprotected;
};

class derived : public base {
public:
void compare(const derived *pb){
int a=pb->basepublic;
a=pb->baseprotected;
}
};

int main(){

return 0;
}

Now when i compile and run this program, it got all through.
MY question is how it is possible to access protected variable of a
different derive object in derive class's member function?
if i change argument of compare function from
const derived *pb to
const base *pb,
i get compilation error
" "1.cpp", line 14: Error: baseprotected is not accessible from
derived::compare(const base*)."
which is expected.
  Reply With Quote


 


2 18th November 15:06
ian collins
External User
 
Posts: 1
Default acces base class member from derive class



Because *pb is a derived and compare is a method of derived. Forget
base members and just consider derived ones, you would expect to be able
to see those, wouldn't you?

--
Ian Collins.
  Reply With Quote
3 23rd November 03:15
gyan
External User
 
Posts: 1
Default acces base class member from derive class


But Pb has been defined in main, outside the scope of derived class, so how
can we access protected member of pb object.member function of a class
should have access only to data member of its own object i.e. "this" and
not other object.
  Reply With Quote
4 21st December 18:31
ian collins
External User
 
Posts: 1
Default acces base class member from derive class


Please quote context. I didn't realise you were replying to me.

In your example that you snipped, main was empty:
#include <iostream.h>

pb is a pointer to a derived, so any other instance of a derived can
fiddle with its private parts.

--
Ian Collins.
  Reply With Quote
Reply


Thread Tools
Display Modes


Some other forums that might be of your interest : Development, Ada, Apple script, Assembler, Awk, Beos, Basic, C, C++, C#, C# .net, .net, .net frameworks, Asp .net, Clarion, Clipper, Clos, Clu, Cobol, Coldfusion, Delphi, Dylan, Eiffel, Forth, Fortran, Haskell, Hermes, Icon, Idl, Java, Java script, Jscript .net, Jcl, Linoleum, Lisp, Lotus, Limbo, Logo, Ml, Mumps, Oberon, Postscript, Pop, Pl1, Prolog, Python, Ruby, Pascal, Perl, Php, Rebol, Rexx, Sed, Sather, Scheme, Smalltalk, Tcl, Vhdl, Vrml, Visual basic, Visual basic .net, Yorick, Mysql, Omnis, Postgresql, Xbase, Access, Oracle, Adabas, Berkeley, Btrieve, Filemaker, Gupta, Db2, Informix, Ingres, Mssql server, Object, Olap, Paradox, Rdb, Revelation, Sybase, Theory, Dbase, Html, Java script, Css, Flash, Photoshop, Corel script, Xml, Tech, Beos, Gem, Hp48, Hpux, Linux, Mac, Ms-dos, Os2, Palm, Solaris, Ti99, Windows, Xenix, Aos, Chorus, Geos, Inferno, Lantastic, Lynx, Mach, Minix, Netware, Os9, Parix, Plan9, Psos, Qnx, Xinu, Sco, Unix, Aix, Aux, 386bsd, Bsdi, Freebsd, Netbsd, Openbsd, Ultrix, Amd, Intel, Aptiva, Buz, Deals, Homebuilt, Overclocking, Programming, Extra forums


Copyright © 2006 SmartyDevil.com - Dies Mies Jeschet Boenedoesef Douvema Enitemaus -
666