Is there a cleaner way to do this?

Discussion in 'C++' started by Peter Olcott, Dec 6, 2005.

  1. Peter Olcott

    Peter Olcott Guest

    //
    // I would like to some how avoid using the global ONE
    // without incurring any performance penalty in speed or space
    //

    #include <vector>
    #include <algorithm>

    struct RecType {
    int AAA;
    int BBB;
    int CCC;
    bool operator<(RecType& Text){ return this->BBB < Text.BBB; };
    };

    struct OneType {
    std::vector<RecType> Record;
    RecType& operator[](const int N){ return Record[N]; };
    } ONE;

    struct TwoType {
    std::vector<int> Sub; // Subscripts of ONE
    bool operator()(const int& N, const int& M);
    void sort(){ std::sort(Sub.begin(), Sub.end(), (*this)); }
    };

    bool TwoType::eek:perator()(const int& N, const int& M) {
    return ONE[N] < ONE[M];
    }
     
    Peter Olcott, Dec 6, 2005
    #1
    1. Advertising

  2. "Peter Olcott" <> schrieb im Newsbeitrag
    news:Rhhlf.11721$QW2.7442@dukeread08...
    > //
    > // I would like to some how avoid using the global ONE
    > // without incurring any performance penalty in speed or space
    > //
    >
    > #include <vector>
    > #include <algorithm>



    class OneTwo
    {
    public:


    >
    > struct RecType {
    > int AAA;
    > int BBB;
    > int CCC;
    > bool operator<(RecType& Text){ return this->BBB < Text.BBB; };
    > };
    >
    > struct OneType {
    > std::vector<RecType> Record;
    > RecType& operator[](const int N){ return Record[N]; };
    > }



    m_ONE;

    >
    > struct TwoType {
    > std::vector<int> Sub; // Subscripts of ONE
    > bool operator()(const int& N, const int& M);
    > void sort(){ std::sort(Sub.begin(), Sub.end(), (*this)); }
    > };
    >
    > bool TwoType::eek:perator()(const int& N, const int& M) {
    > return m_ONE[N] < m_ONE[M];
    > }

    };
     
    Gernot Frisch, Dec 6, 2005
    #2
    1. Advertising

  3. Peter Olcott

    Peter Olcott Guest

    That looks like just the sort of solution that I was looking for.
    Thanks again.

    "Gernot Frisch" <> wrote in message
    news:...
    >
    > "Peter Olcott" <> schrieb im Newsbeitrag
    > news:Rhhlf.11721$QW2.7442@dukeread08...
    >> //
    >> // I would like to some how avoid using the global ONE
    >> // without incurring any performance penalty in speed or
    >> space
    >> //
    >>
    >> #include <vector>
    >> #include <algorithm>

    >
    >
    > class OneTwo
    > {
    > public:
    >
    >
    >>
    >> struct RecType {
    >> int AAA;
    >> int BBB;
    >> int CCC;
    >> bool operator<(RecType& Text){ return this->BBB <
    >> Text.BBB; };
    >> };
    >>
    >> struct OneType {
    >> std::vector<RecType> Record;
    >> RecType& operator[](const int N){ return Record[N]; };
    >> }

    >
    >
    > m_ONE;
    >
    >>
    >> struct TwoType {
    >> std::vector<int> Sub; // Subscripts of ONE
    >> bool operator()(const int& N, const int& M);
    >> void sort(){ std::sort(Sub.begin(), Sub.end(), (*this)); }
    >> };
    >>
    >> bool TwoType::eek:perator()(const int& N, const int& M) {
    >> return m_ONE[N] < m_ONE[M];
    >> }

    > };
    >
    >
    >
     
    Peter Olcott, Dec 6, 2005
    #3
  4. Peter Olcott

    Peter Olcott Guest

    The code will not compile.

    "Gernot Frisch" <> wrote in message
    news:...
    >
    > "Peter Olcott" <> schrieb im Newsbeitrag
    > news:Rhhlf.11721$QW2.7442@dukeread08...
    >> //
    >> // I would like to some how avoid using the global ONE
    >> // without incurring any performance penalty in speed or
    >> space
    >> //
    >>
    >> #include <vector>
    >> #include <algorithm>

    >
    >
    > class OneTwo
    > {
    > public:
    >
    >
    >>
    >> struct RecType {
    >> int AAA;
    >> int BBB;
    >> int CCC;
    >> bool operator<(RecType& Text){ return this->BBB <
    >> Text.BBB; };
    >> };
    >>
    >> struct OneType {
    >> std::vector<RecType> Record;
    >> RecType& operator[](const int N){ return Record[N]; };
    >> }

    >
    >
    > m_ONE;
    >
    >>
    >> struct TwoType {
    >> std::vector<int> Sub; // Subscripts of ONE
    >> bool operator()(const int& N, const int& M);
    >> void sort(){ std::sort(Sub.begin(), Sub.end(), (*this)); }
    >> };
    >>
    >> bool TwoType::eek:perator()(const int& N, const int& M) {
    >> return m_ONE[N] < m_ONE[M];
    >> }

    > };
    >
    >
    >


    #include <stdio.h>
    #include <vector>
    #include <algorithm>


    class OneTwo {
    public:

    struct RecType {
    int AAA;
    int BBB;
    int CCC;
    bool operator<(RecType& Text){ return this->BBB <
    Text.BBB; };
    };

    struct OneType {
    std::vector<RecType> Record;
    RecType& operator[](const int N){ return Record[N]; };
    } ONE;

    struct TwoType {
    std::vector<int> Sub; // Subscripts of ONE
    bool operator()(const int& N, const int& M)
    { return ONE[N] < ONE[M]; };
    void sort(){ std::sort(Sub.begin(), Sub.end(), (*this)); };
    };

    //std::vector<TwoType> StackTwoType;
    };



    void main() {
    }


    t.cpp
    t.cpp(24) : error C2327: 'OneTwo::ONE' : member from enclosing
    class is not a type name, static, or enumerator
    t.cpp(24) : error C2065: 'ONE' : undeclared identifier
    t.cpp(24) : error C2109: subscript requires array or pointer type
    t.cpp(24) : error C2327: 'OneTwo::ONE' : member from enclosing
    class is not a type name, static, or enumerator
    t.cpp(24) : error C2109: subscript requires array or pointer type
    t.cpp(35) : error C2143: syntax error : missing ';' before '.'
    t.cpp(35) : error C2501: 't' : missing storage-class or type
    specifiers
    t.cpp(35) : error C2143: syntax error : missing ';' before '.'
     
    Peter Olcott, Dec 6, 2005
    #4
  5. Peter Olcott wrote:

    > The code will not compile.
    >
    > "Gernot Frisch" <> wrote in message
    > news:...
    >
    >>"Peter Olcott" <> schrieb im Newsbeitrag
    >>news:Rhhlf.11721$QW2.7442@dukeread08...
    >>
    >>>//
    >>>// I would like to some how avoid using the global ONE
    >>>// without incurring any performance penalty in speed or
    >>>space
    >>>//
    >>>
    >>>#include <vector>
    >>>#include <algorithm>

    >>
    >>
    >>class OneTwo
    >>{
    >>public:
    >>
    >>
    >>
    >>>struct RecType {
    >>> int AAA;
    >>> int BBB;
    >>> int CCC;
    >>> bool operator<(RecType& Text){ return this->BBB <
    >>>Text.BBB; };
    >>>};
    >>>
    >>>struct OneType {
    >>> std::vector<RecType> Record;
    >>> RecType& operator[](const int N){ return Record[N]; };
    >>>}

    >>
    >>
    >>m_ONE;
    >>
    >>
    >>>struct TwoType {
    >>> std::vector<int> Sub; // Subscripts of ONE
    >>> bool operator()(const int& N, const int& M);
    >>> void sort(){ std::sort(Sub.begin(), Sub.end(), (*this)); }
    >>>};
    >>>
    >>>bool TwoType::eek:perator()(const int& N, const int& M) {
    >>> return m_ONE[N] < m_ONE[M];
    >>>}

    >>
    >>};
    >>
    >>
    >>

    >
    >
    > #include <stdio.h>
    > #include <vector>
    > #include <algorithm>
    >
    >
    > class OneTwo {
    > public:
    >
    > struct RecType {
    > int AAA;
    > int BBB;
    > int CCC;
    > bool operator<(RecType& Text){ return this->BBB <
    > Text.BBB; };
    > };
    >
    > struct OneType {
    > std::vector<RecType> Record;
    > RecType& operator[](const int N){ return Record[N]; };
    > } ONE;


    Here 'ONE' is declared a member of 'OneTwo'.

    >
    > struct TwoType {
    > std::vector<int> Sub; // Subscripts of ONE
    > bool operator()(const int& N, const int& M)
    > { return ONE[N] < ONE[M]; };


    Here you try to use 'ONE' inside of 'TwoType' (which is unrelated to
    'OneTwo', only defined inside it). Of course 'ONE' is undeclared here.

    Are you coming from Javaland, where members of enclosing class are also
    members of the inner classes and instances of inner classes are also
    data members of the enclosing classes? It's not the C++ way. You need
    an instance of 'OneTwo' to access its members.

    > void sort(){ std::sort(Sub.begin(), Sub.end(), (*this)); };
    > };
    >
    > //std::vector<TwoType> StackTwoType;
    > };
    >
    >
    >
    > void main() {
    > }
    >
    >
    > t.cpp
    > t.cpp(24) : error C2327: 'OneTwo::ONE' : member from enclosing
    > class is not a type name, static, or enumerator
    > t.cpp(24) : error C2065: 'ONE' : undeclared identifier
    > t.cpp(24) : error C2109: subscript requires array or pointer type
    > t.cpp(24) : error C2327: 'OneTwo::ONE' : member from enclosing
    > class is not a type name, static, or enumerator
    > t.cpp(24) : error C2109: subscript requires array or pointer type
    > t.cpp(35) : error C2143: syntax error : missing ';' before '.'
    > t.cpp(35) : error C2501: 't' : missing storage-class or type
    > specifiers
    > t.cpp(35) : error C2143: syntax error : missing ';' before '.'
    >
    >


    V
     
    Victor Bazarov, Dec 6, 2005
    #5
  6. Peter Olcott

    Peter Olcott Guest

    I am still working on the same problem that I talked to you about
    last night. This code was the suggestion of Gernot Frisch.

    "Victor Bazarov" <> wrote in message
    news:ypjlf.54690$01.us.to.verio.net...
    > Peter Olcott wrote:
    >
    >> The code will not compile.
    >>
    >> "Gernot Frisch" <> wrote in message
    >> news:...
    >>
    >>>"Peter Olcott" <> schrieb im Newsbeitrag
    >>>news:Rhhlf.11721$QW2.7442@dukeread08...
    >>>
    >>>>//
    >>>>// I would like to some how avoid using the global ONE
    >>>>// without incurring any performance penalty in speed or
    >>>>space
    >>>>//
    >>>>
    >>>>#include <vector>
    >>>>#include <algorithm>
    >>>
    >>>
    >>>class OneTwo
    >>>{
    >>>public:
    >>>
    >>>
    >>>
    >>>>struct RecType {
    >>>> int AAA;
    >>>> int BBB;
    >>>> int CCC;
    >>>> bool operator<(RecType& Text){ return this->BBB <
    >>>> Text.BBB; };
    >>>>};
    >>>>
    >>>>struct OneType {
    >>>> std::vector<RecType> Record;
    >>>> RecType& operator[](const int N){ return Record[N]; };
    >>>>}
    >>>
    >>>
    >>>m_ONE;
    >>>
    >>>
    >>>>struct TwoType {
    >>>> std::vector<int> Sub; // Subscripts of ONE
    >>>> bool operator()(const int& N, const int& M);
    >>>> void sort(){ std::sort(Sub.begin(), Sub.end(), (*this)); }
    >>>>};
    >>>>
    >>>>bool TwoType::eek:perator()(const int& N, const int& M) {
    >>>> return m_ONE[N] < m_ONE[M];
    >>>>}
    >>>
    >>>};
    >>>
    >>>
    >>>

    >>
    >>
    >> #include <stdio.h>
    >> #include <vector>
    >> #include <algorithm>
    >>
    >>
    >> class OneTwo {
    >> public:
    >>
    >> struct RecType {
    >> int AAA;
    >> int BBB;
    >> int CCC;
    >> bool operator<(RecType& Text){ return this->BBB <
    >> Text.BBB; };
    >> };
    >>
    >> struct OneType {
    >> std::vector<RecType> Record;
    >> RecType& operator[](const int N){ return Record[N]; };
    >> } ONE;

    >
    > Here 'ONE' is declared a member of 'OneTwo'.
    >
    >>
    >> struct TwoType {
    >> std::vector<int> Sub; // Subscripts of ONE
    >> bool operator()(const int& N, const int& M)
    >> { return ONE[N] < ONE[M]; };

    >
    > Here you try to use 'ONE' inside of 'TwoType' (which is
    > unrelated to
    > 'OneTwo', only defined inside it). Of course 'ONE' is
    > undeclared here.
    >
    > Are you coming from Javaland, where members of enclosing class
    > are also
    > members of the inner classes and instances of inner classes are
    > also
    > data members of the enclosing classes? It's not the C++ way.
    > You need
    > an instance of 'OneTwo' to access its members.
    >
    >> void sort(){ std::sort(Sub.begin(), Sub.end(),
    >> (*this)); };
    >> };
    >>
    >> //std::vector<TwoType> StackTwoType;
    >> };
    >>
    >>
    >>
    >> void main() {
    >> }
    >>
    >>
    >> t.cpp
    >> t.cpp(24) : error C2327: 'OneTwo::ONE' : member from enclosing
    >> class is not a type name, static, or enumerator
    >> t.cpp(24) : error C2065: 'ONE' : undeclared identifier
    >> t.cpp(24) : error C2109: subscript requires array or pointer
    >> type
    >> t.cpp(24) : error C2327: 'OneTwo::ONE' : member from enclosing
    >> class is not a type name, static, or enumerator
    >> t.cpp(24) : error C2109: subscript requires array or pointer
    >> type
    >> t.cpp(35) : error C2143: syntax error : missing ';' before '.'
    >> t.cpp(35) : error C2501: 't' : missing storage-class or type
    >> specifiers
    >> t.cpp(35) : error C2143: syntax error : missing ';' before '.'
    >>
    >>

    >
    > V
     
    Peter Olcott, Dec 6, 2005
    #6
  7. class OneTwo
    {
    struct RecType {
    int AAA;
    int BBB;
    int CCC;
    bool operator<(RecType& Text){ return this->BBB < Text.BBB; };
    };


    struct OneType {
    std::vector<RecType> Record;
    RecType& operator[](const int N){ return Record[N]; };
    }m_ONE;

    struct TwoType
    {
    TwoType(OneType& ot): m_ot(ot) {}
    OneType& m_ot;
    std::vector<int> Sub; // Subscripts of ONE
    bool operator()(const int& N, const int& M);
    void sort() { std::sort(Sub.begin(), Sub.end(), (*this)); }
    };
    };

    bool OneTwo::TwoType::eek:perator()(const int& N, const int& M)
    {
    return m_ot[N] < m_ot[M];
    }


    Now you have to provide a OneType when creating a TwoType. Can you
    live with that?
     
    Gernot Frisch, Dec 7, 2005
    #7
  8. Peter Olcott

    Peter Olcott Guest

    "Gernot Frisch" <> wrote in message
    news:...
    > class OneTwo
    > {
    > struct RecType {
    > int AAA;
    > int BBB;
    > int CCC;
    > bool operator<(RecType& Text){ return this->BBB < Text.BBB; };
    > };
    >
    >
    > struct OneType {
    > std::vector<RecType> Record;
    > RecType& operator[](const int N){ return Record[N]; };
    > }m_ONE;
    >
    > struct TwoType
    > {
    > TwoType(OneType& ot): m_ot(ot) {}
    > OneType& m_ot;
    > std::vector<int> Sub; // Subscripts of ONE
    > bool operator()(const int& N, const int& M);
    > void sort() { std::sort(Sub.begin(), Sub.end(), (*this)); }
    > };
    > };
    >
    > bool OneTwo::TwoType::eek:perator()(const int& N, const int& M)
    > {
    > return m_ot[N] < m_ot[M];
    > }
    >
    >
    > Now you have to provide a OneType when creating a TwoType. Can
    > you live with that?
    >
    >
    >
    >


    I am not sure. If it would actually construct a copy of my
    OneType it won't be feasible. OneType is essentially a singleton.
    It can have hundreds of megabytes of data. If what you are
    proposing is the same thing as copying a pointer, then it could
    work, yet might not be worth the extra overhead. From what I
    understand references can work like pointers yet are processed at
    compile time, rather than run-time. What I have done in the mean
    time is to rename OneType to GlobalOneType, and then passed this
    as a parameter. In other words I have minimized the global access
    to GlobalOneType, and made the remaining access explicitly clear
    that it is global.
     
    Peter Olcott, Dec 7, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Salerno
    Replies:
    13
    Views:
    489
    Boris Borcic
    Aug 3, 2006
  2. John Salerno

    cleaner way to write this?

    John Salerno, Oct 25, 2006, in forum: Python
    Replies:
    29
    Views:
    602
    John Salerno
    Oct 26, 2006
  3. Darrel

    Cleaner way to write this?

    Darrel, Mar 20, 2007, in forum: ASP .Net
    Replies:
    1
    Views:
    280
    Cowboy \(Gregory A. Beamer\)
    Mar 20, 2007
  4. Fencer
    Replies:
    6
    Views:
    358
    Fencer
    Jul 29, 2010
  5. Ron M
    Replies:
    22
    Views:
    255
    Martin DeMello
    Oct 30, 2005
Loading...

Share This Page