Hi,

I wondered if it was possible to declare the friends of class but out of it?
Like this:

You have created class:

class myclass{
          int x;
        public:
bla bla bla
};

And instead of this:

class myclass{
          int x;
        public:
bla bla bla
friend class someclass;
};

If it possible to do something like this:

class myclass{
          int x;
        public:
bla bla bla
};

//this is out of class

myclass::friend class someclass;

I know the last line is piece of crap, but is there any way to do something like that?

I need this because I started working in Borland 6 and I created class but can't use it since it can't recognize objects like Memo1.

Thanks!

Regardless of which compiler you're using, how are you declaring your class, and how do you expect to use it? Anything you wish to access from outside the class implementation should be declared public: ... by default, the contents of a C++ class are private: . There's also a protected: declaration that allows access from derived subclasses.

class MyClass
{
private:  // this is already the case
    int intVal;
    std::string strVal;

public:  // these are the parts you want outside access to
    MyClass();
    MyClass(int anIntVal, const std::string &aStrVal);
    MyClass(const MyClass &other);
    ~MyClass();

    void setIntVal(int anIntVal);
    int getIntVal() const;

    ...

private:
    // more private stuff

...
};

The point of "friend" is so that a class can tell other classes that they have access to non-public members, purely as a convenience. If you could do that from outside a class, then you defeat the reason to have any privacy at all (and the reason is to enforce OO notions of encapsulation/abstraction).

Yes, I know what's it about. But I was confused starting programming in Borland so some crazy ideas came to me ( like that one ).
I actually solved my problem!
Thanks anyway!

This article has been dead for over six months. Start a new discussion instead.