Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Is there a cleaner way to do this?

Reply
Thread Tools

Is there a cleaner way to do this?

 
 
Peter Olcott
Guest
Posts: n/a
 
      12-06-2005
//
// 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:perator()(const int& N, const int& M) {
return ONE[N] < ONE[M];
}


 
Reply With Quote
 
 
 
 
Gernot Frisch
Guest
Posts: n/a
 
      12-06-2005

"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:perator()(const int& N, const int& M) {
> return m_ONE[N] < m_ONE[M];
> }

};



 
Reply With Quote
 
 
 
 
Peter Olcott
Guest
Posts: n/a
 
      12-06-2005
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:perator()(const int& N, const int& M) {
>> return m_ONE[N] < m_ONE[M];
>> }

> };
>
>
>



 
Reply With Quote
 
Peter Olcott
Guest
Posts: n/a
 
      12-06-2005
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: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 '.'


 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      12-06-2005
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: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
 
Reply With Quote
 
Peter Olcott
Guest
Posts: n/a
 
      12-06-2005

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$ o.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: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



 
Reply With Quote
 
Gernot Frisch
Guest
Posts: n/a
 
      12-07-2005
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: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?




 
Reply With Quote
 
Peter Olcott
Guest
Posts: n/a
 
      12-07-2005

"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: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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
cleaner way to write this? John Salerno Python 29 10-26-2006 03:03 PM
cleaner way to write this try/except statement? John Salerno Python 13 08-03-2006 07:04 PM
glass cleaner damaged my DLP 60" tv screen -- any way to fix it??? Beowulf DVD Video 47 05-05-2006 04:57 AM
What's the best registry cleaner out there?? PW Computer Support 2 04-27-2006 01:01 PM
Cleaner syntax for .map (is there already a way, or ruby2 idea?) Ron M Ruby 22 10-30-2005 10:41 AM



Advertisments