Click here to Skip to main content
15,896,726 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
package concept in JAVA;
1. By default all classes/func/members with in the same package, they can access each other symbols/data, aside from tagged private access modifiers, and without explicitly importing them.
2. ...
3. ...
...


What I have tried:

We only need concept number #1, and it is ok if we cannot implicitly import/using a package/namespace, so I tried this.

C++
namespace com::tut::cpp {
    using com::tut::cpp::Derive; //REM: I tried applying: using-declarations or using-directives
    class Base {
    public:
        Base();
        virtual ~Base();
        Derive& funcBase(); //REM: it return a instantiated Derive class
    };

    class Derive : public Base {
    public:
        Derive();
        ~Derive() override;
    };
}


//REM: Definition
com::tut::cpp::Base::Base() {}
com::tut::cpp::Base::~Base() {}
com::tut::cpp::Derive& com::tut::cpp::Base::funcBase() {
    static com::tut::cpp::Derive d;
    return d;
}

com::tut::cpp::Derive::Derive() {}
com::tut::cpp::Derive::~Derive() {}


And we would like to separate them into different files, such as;
//Base.h
#include "Derive.h"
//using com::tut::cpp::Derive; //REM: is it possible to do this outside?
namespace com::tut::cpp {
    using com::tut::cpp::Derive;
    class Base {
    public:
        Base();
        virtual ~Base();
        Derive& funcBase();
    };

}
//Derive.h
#include "Base.h"
namespace com::tut::cpp {
    class Derive : public Base {
    public:
        Derive();
        ~Derive() override;
    };
}
//Body.cpp
#include "Base.h"
#include "Derive.h"
//REM: Definition
com::tut::cpp::Base::Base() {}
com::tut::cpp::Base::~Base() {}
com::tut::cpp::Derive& com::tut::cpp::Base::funcBase() {
    static com::tut::cpp::Derive d;
    return d;
}

com::tut::cpp::Derive::Derive() {}
com::tut::cpp::Derive::~Derive() {}
Posted
Updated 22-May-22 2:34am
v4
Comments
Phoenix Liveon 22-May-22 4:39am    
In C++20, the modular system, can we do this kind of concept without an error saying "found cyclic-dependency".
Phoenix Liveon 22-May-22 5:09am    
Added: "And it still separated by a file"
Richard MacCutchan 22-May-22 7:42am    
It would be easier if you did not use such a complex structure. A single level namespace (or none) would make your code easier to read and maintain.
Phoenix Liveon 22-May-22 8:26am    
thanks & noted.
Richard MacCutchan 22-May-22 8:41am    
Don't try to emulate features from one language into another; it rarely proves anything but troublesome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900