Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   functor object in template class (http://www.velocityreviews.com/forums/t267687-functor-object-in-template-class.html)

Chandra Shekhar Kumar 06-24-2003 06:33 PM

Re: functor object in template class
 
introduce a public member function like getVal for getting the value i then call this
function in write_row2database


porschberg 06-25-2003 05:59 AM

functor object in template class
 
Hi,
I have a template class that looks like:

template <class T>
class Update
{
friend otl_stream& operator<< (otl_stream&, const shared_ptr<typename T::row>&);
friend ostream& operator<< (ostream&, const shared_ptr<typename T::row>&);
friend struct write_row2database;
public:
typedef shared_ptr<typename T::row> ROW;
typedef typename vector<ROW>::iterator iter;
typedef typename vector<ROW>::value_type val;

....

struct display_row {
void operator()(val& aVal) {
cout << aVal;
}
};

struct write_row2database {
void operator()(val& aVal) {
// (Update<T>).i << aVal;
// !!!!! the problem !!!!!
// i is (private) member of template class, how to address ?
}

....
bool flush(string& error)
{
for_each(v.begin(), v.end(), display_row()); //works
for_each(v.begin(), v.end(), write_row2database());
}

private:
...
vector<ROW> v;
otl_nocommit_stream i;
...

};

My question:
How can I write to i inside write_row2database ,
what is the correct syntax?
I declared
friend struct write_row2database;
and there exist a operator overloading for "otl_stream<<theROW".

Looking wishful forward to a reply,
Thomas

Rob Williscroft 06-25-2003 07:17 AM

Re: functor object in template class
 
porschberg wrote in
news:8d9566f5.0306242159.619d665e@posting.google.c om:

> Hi,
> I have a template class that looks like:
>
> template <class T>
> class Update
> {
> friend otl_stream& operator<<
> (otl_stream&, const shared_ptr<typename T::row>&);
> friend ostream& operator<<
> (ostream&,const shared_ptr<typename T::row>&);


// insert this:

struct write_row2database;

> friend struct write_row2database;


/* without the inserted forward declation the
write_row2database here referes to ::write_row2database
not Update<T>::write_row2database
*/

>public:

[snip]
> struct write_row2database {
> void operator()(val& aVal) {
> // (Update<T>).i << aVal;
> // !!!!! the problem !!!!!
> // i is (private) member of template class, how to address ?
> }
>

[snip]

HTH

Rob.
--
http://www.victim-prime.dsl.pipex.com/

porschberg 06-25-2003 01:53 PM

Re: functor object in template class
 
Rob Williscroft <rtw@freenet.REMOVE.co.uk> wrote in message news:<Xns93A5543576473ukcoREMOVEfreenetrtw@195.129 .110.201>...
> porschberg wrote in
> news:8d9566f5.0306242159.619d665e@posting.google.c om:
>
> > Hi,
> > I have a template class that looks like:
> >
> > template <class T>
> > class Update
> > {
> > friend otl_stream& operator<<
> > (otl_stream&, const shared_ptr<typename T::row>&);
> > friend ostream& operator<<
> > (ostream&,const shared_ptr<typename T::row>&);

>
> // insert this:
>
> struct write_row2database;
>
> > friend struct write_row2database;

>
> /* without the inserted forward declation the
> write_row2database here referes to ::write_row2database
> not Update<T>::write_row2database
> */
>
> >public:

> [snip]
> > struct write_row2database {
> > void operator()(val& aVal) {
> > // (Update<T>).i << aVal;
> > // !!!!! the problem !!!!!
> > // i is (private) member of template class, how to address ?
> > }
> >

> [snip]
>
> HTH
>
> Rob.

Hi Rob, the forward declaration did not help me.
I think the line (Update<T>).i << aVal; is wrong.
compiler says:
Update.hh:101: error: parse error before `;' token
If I change the line to
i << aVal;
I get:
Update.hh:101: error: 'struct
Update<P_WHReasonUpdate>::write_row2database' has no member named 'i'
what I understand.
My aim was to replace the handwritten loop with the for_each
algorithm but now I see no way to foist this i to the function object.

Victor Bazarov 06-25-2003 02:28 PM

Re: functor object in template class
 
"porschberg" <thomas.porschberg@osp-dd.de> wrote...
> Hi,
> I have a template class that looks like:
>
> template <class T>
> class Update
> {
> friend otl_stream& operator<< (otl_stream&, const shared_ptr<typename

T::row>&);
> friend ostream& operator<< (ostream&, const shared_ptr<typename

T::row>&);
> friend struct write_row2database;
> public:
> typedef shared_ptr<typename T::row> ROW;
> typedef typename vector<ROW>::iterator iter;
> typedef typename vector<ROW>::value_type val;
>
> ...
>
> struct display_row {
> void operator()(val& aVal) {
> cout << aVal;
> }
> };
>
> struct write_row2database {
> void operator()(val& aVal) {
> // (Update<T>).i << aVal;
> // !!!!! the problem !!!!!
> // i is (private) member of template class, how to address ?
> }


};

Hereby lies the problem. Class 'write_row2database', though declared
nested to "Update" template, knows NOTHING about the Update object
to which you attempt to output the 'aVal'. What you need to do is
to create 'write_row2database' so that it know what 'Update' object
to use:

struct write_row2database {
Update& upd;
write_row2database(Update& u) : upd(u) {}
void operator()(val& aVal) {
upd.i << aVal;
}
};

>
> ...
> bool flush(string& error)
> {
> for_each(v.begin(), v.end(), display_row()); //works
> for_each(v.begin(), v.end(), write_row2database());


And here you HAVE to pass the Update object for which the functor
is created:

for_each(v.begin(), v.end(), write_row2database(*this));

> }
>
> private:
> ...
> vector<ROW> v;
> otl_nocommit_stream i;
> ...
>
> };
>
> My question:
> How can I write to i inside write_row2database ,
> what is the correct syntax?
> I declared
> friend struct write_row2database;
> and there exist a operator overloading for "otl_stream<<theROW".


Victor



porschberg 06-26-2003 04:55 AM

Re: functor object in template class
 
Victor thanks a lot! I understand and it works now.
thomas
"Victor Bazarov" <v.Abazarov@attAbi.com> wrote in message news:<vfjccbadca038b@corp.supernews.com>...
> "porschberg" <thomas.porschberg@osp-dd.de> wrote...
> > Hi,
> > I have a template class that looks like:
> >
> > template <class T>
> > class Update
> > {
> > friend otl_stream& operator<< (otl_stream&, const shared_ptr<typename

> T::row>&);
> > friend ostream& operator<< (ostream&, const shared_ptr<typename

> T::row>&);
> > friend struct write_row2database;
> > public:
> > typedef shared_ptr<typename T::row> ROW;
> > typedef typename vector<ROW>::iterator iter;
> > typedef typename vector<ROW>::value_type val;
> >
> > ...
> >
> > struct display_row {
> > void operator()(val& aVal) {
> > cout << aVal;
> > }
> > };
> >
> > struct write_row2database {
> > void operator()(val& aVal) {
> > // (Update<T>).i << aVal;
> > // !!!!! the problem !!!!!
> > // i is (private) member of template class, how to address ?
> > }

>
> };
>
> Hereby lies the problem. Class 'write_row2database', though declared
> nested to "Update" template, knows NOTHING about the Update object
> to which you attempt to output the 'aVal'. What you need to do is
> to create 'write_row2database' so that it know what 'Update' object
> to use:
>
> struct write_row2database {
> Update& upd;
> write_row2database(Update& u) : upd(u) {}
> void operator()(val& aVal) {
> upd.i << aVal;
> }
> };
>
> >
> > ...
> > bool flush(string& error)
> > {
> > for_each(v.begin(), v.end(), display_row()); //works
> > for_each(v.begin(), v.end(), write_row2database());

>
> And here you HAVE to pass the Update object for which the functor
> is created:
>
> for_each(v.begin(), v.end(), write_row2database(*this));
>
> > }
> >
> > private:
> > ...
> > vector<ROW> v;
> > otl_nocommit_stream i;
> > ...
> >
> > };
> >
> > My question:
> > How can I write to i inside write_row2database ,
> > what is the correct syntax?
> > I declared
> > friend struct write_row2database;
> > and there exist a operator overloading for "otl_stream<<theROW".

>
> Victor



All times are GMT. The time now is 05:35 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57