Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

public inheritance can emulate public/private

Name: Anonymous 2012-02-18 18:14

class A has three different interfaces, based upon what type of access you have to it.


class A {
public:
  int thisIntIsPublic;
  int thisMethodIsPublic(int x);
protected:
  int thisIntIsProtected;
  int thisMethodIsProtected(int x);
private:
  int thisIntIsPrivate;
  int thisMethodIsPrivate(int x);
};


Why not factor these interfaces into three different data types? That way, more than one data type can implement the public interface, while having different implementations for the protected and or private interfaces?


struct A {
  int thisIntIsPublic;
  int thisMethodIsPublic(int x);
};

struct A_protected : public A {
  int thisIntIsProtected;
  int thisMethodIsProtected(int x);
};

struct A_private : public A_protected {
  int thisIntIsPrivate;
  int thisMethodIsPrivate(int x);
};


struct OtherAImplementation_protected : public A {
  char differentMemberHere;
  double differentMethodThere(int x, int y);
};

struct OtherAAImplementation_private : public OtherAImplementation_protected {
  ...
};


The disadvantage is not having the protected/private data access enforced on the language level. You would just need to only use a A_protected pointer when you have protected Access to A,, and only use a A_private pointer when you had private access to A. So the enforcement would be in following proper naming conventions. You would also need to instantiate A as an instance of the private class:


A* instance = new A_private(...);

Name: Anonymous 2012-02-18 18:57

pubic inheritance

Name: Anonymous 2012-02-18 20:03

never mind this doesn't really work when public methods call protected methods, which is pretty much exactly what they should always do. It could work with containment though, by making the public object contain a reference to the protected object.


class A {
public:
  int blah(int x) {
    return bleh(x + 4);
  }
protected:
  int bleh(int x) {
    return x*8;
  }
};

int main(void) {
  A* instance = new A();
  A.blah(); // works
  A.bleh(); // cannot access protected member.
  delete A;
}



struct A {
  A(A_protected* protectedThis) : protectedThis(protectedThis)
  ~A() { delete protectedThis; } // Possibly use reference counting if protected instance may be shared.
  int blah(int x) {
    return protectedThis->bleh(x + 4);
  }
  A_protected* protectedThis; // don't access this field, plox
};

struct A_protected {
  int bleh(int x) {
    return x*8;
  }
};

int main(void) {
  A* instance = new A(new A_protected());
  A.blah(); // works
  A.bleh(); // bleh isn't defined in A.
            // please don't write A->protectedThis->bleh(); please.
  delete A;
}

Name: Anonymous 2012-02-18 20:04

The real question is why are you posting some stupid piece of shit C++ code in /prog/?

Name: Anonymous 2012-02-18 20:06

>>4
who is providing the question, and who is meant to provide the answer?

Name: Anonymous 2012-02-18 20:17

ok now it should work.


struct A {
  virtual ~A() {}
  int blah(int x) = 0;
};

struct A_protected {
  virtual ~A_protected() {}
  int bleh(int x) = 0;
};

struct A1 : public A {
  A1(A_protected* protectedThis) : protectedThis(protectedThis) {}
  virtual ~A1() { delete protectedThis; }
  int blah(int x) { return protectedThis->bleh(x + 4); }
  A_protected* protectedThis;
};

struct A1_protected : public A_protected {
  virtual A1_protected() {}
  int bleh(int x) { return x*8; }
};

int main(void) {
  A* instance = new A1(new A1_protected());
  A->blah(); // works
  A->bleh(); // bleh isn't defined in A, and you can't
            // get a hold of the protected instance
            // without using a cast down to A1.
  delete A;
}


now you can mix and match public and protected implementations:


 A* instance = new A1(new A2_protected());


as long as A2_protected inherits from A_protected, it'll work.

Name: Anonymous 2012-11-28 7:36

>>2
fiend class

Name: Anonymous 2012-11-28 10:02

>>6
The almighty wonders of C++.

Don't change these.
Name: Email:
Entire Thread Thread List