unsure how to implement MFC CArray with class

M

Mike Stenzler

I am new to Template programming and I would like to create an array of
user-defined class objects using MFC CArray. Normally I would use linked
list processing - create an object class and then an object-list class,
but in this instance array processing would be more efficient. However I
need to use arrays that can dynamically shrink and grow ala Visual Basic
arrays.

I'm confused as to how to declare/implement the CArray using my Class.

class mydatastruct
{
public:
mydatastruct();
~mydatastruct();
private:
CString m_date;
double m_value;
};

How would I declare an instance of a CArray of mydatastructs? I'm
thinking this is something I should be able to do...

Mike
 
M

Mike Wahler

Mike Stenzler said:
I am new to Template programming and I would like to create an array of
user-defined class objects using MFC CArray. Normally I would use linked
list processing - create an object class and then an object-list class,
but in this instance array processing would be more efficient. However I
need to use arrays that can dynamically shrink and grow ala Visual Basic
arrays.

The standard container std::vector can do this.
I recommend it over an implementation-specific
one like an MFC type.
I'm confused as to how to declare/implement the CArray using my Class.

class mydatastruct
{
public:
mydatastruct();
~mydatastruct();
private:
CString m_date;
double m_value;
};

How would I declare an instance of a CArray of mydatastructs?

Read the CArray documenation. The standard C++ language
(the only topic here) has nothing to say about it.
I'm
thinking this is something I should be able to do...

Yes, I'm sure it is. Haven't you read your documentation?
It should be provided with your Windows compiler, and
also at www.msdn.microsoft.com

I still recommend a std::vector, though, it can do
anything an MFC CArray can do, and will work anywhere
there's a standard C++ implementation.

-Mike
 
M

Mike Stenzler

Mike said:
The standard container std::vector can do this.
I recommend it over an implementation-specific
one like an MFC type.

OK, I'll bite ...

If I were to use std::vector instead of CArray, how would I implement a simple
class as a dynamically sizeable array of those class objects?
Read the CArray documenation. The standard C++ language
(the only topic here) has nothing to say about it.


Yes, I'm sure it is. Haven't you read your documentation?
It should be provided with your Windows compiler, and

// small rant below

I understand about posting to the wrong newsgroup for CArray-specific help.
Mea Culpa, my apologies. As for reading the documentation - after almost 20
years as a professional programmer writing in 6 languages under I'm not sure
how many different OS environments, I've learned that not all documentation is
equal. Sometimes RTFM isn't the appropriate response to a request for some
insight into how to use/do things. Having 2 small children and job deadline
pressures to meet, I usually don't have the luxury of poring through doco and
"teaching myself" when I'm trying to implement a technique that's new to me.
That's when I rely on asking others who have the knowledge I need - I thought
that was a big component of what Usenet was all about - people sharing
knowlege.
I still recommend a std::vector, though, it can do
anything an MFC CArray can do, and will work anywhere
there's a standard C++ implementation.

Like I said, I'll bite. How do you use it?

Thanks

Mike Stenzler
 
A

Attila Feher

Mike Stenzler wrote:
[SNIP]
Like I said, I'll bite. How do you use it?

Do you see online help in the name of this newsgroup? There are million
places where the standard vector class is described, with example programs.
Including the help facility of the Microsoft compilers and the
online/offline MSDN.
 
A

Attila Feher

Attila Feher wrote:
[SNIP]
There are million places where the standard vector class is
described, with example programs. Including the help facility
of the Microsoft compilers and the online/offline MSDN.

IOW: show some effort please.
 
J

Jonathan Mcdougall

The standard container std::vector can do this.
OK, I'll bite ...

If I were to use std::vector instead of CArray, how would I implement a simple
class as a dynamically sizeable array of those class objects?


# include <vector>
# include <iostream>

using std::vector;

class my_class
{
public:
void f()
{
std::cout << "hello" << std::endl;
}
};

int main()
{
vector<my_class> v; // a vector of my_class'es

v.push_back(my_class());
v.push_back(my_class());

// v now contains two objects

v.pop_back();

// v now contains two objects

// inserting a new object in front
v.insert(v.begin(), my_class());

v[0].f(); // "hello"
v[1].f(); // "hello"
}

Read about containers and iterators, and get The C++ Standard
Library by Josuttis.
// small rant below

I understand about posting to the wrong newsgroup for CArray-specific help.
Mea Culpa, my apologies. As for reading the documentation - after almost 20
years as a professional programmer writing in 6 languages under I'm not sure
how many different OS environments, I've learned that not all documentation is
equal. Sometimes RTFM isn't the appropriate response to a request for some
insight into how to use/do things. Having 2 small children and job deadline
pressures to meet, I usually don't have the luxury of poring through doco and
"teaching myself" when I'm trying to implement a technique that's new to me.
That's when I rely on asking others who have the knowledge I need - I thought
that was a big component of what Usenet was all about - people sharing
knowlege.

I suppose if you don't have the time to read your documentation, we don't
have
the time to help you.


Jonathan
 
M

Mike Stenzler

WW said:
Mike said:
I am new to Template programming and I would like to create an array
of user-defined class objects using MFC CArray. Normally I would use
[SNIP]

FAQ: "Which newsgroup should I post my questions?"
http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.9

CArray, CString etc. are Microsoft propriatery classes from MFC and are
off-topic here. If you decide to use std::vector, this will be the right
place to post to.
Attila Feher wrote:
Do you see online help in the name of this newsgroup? There are million
places where the standard vector class is described, with example programs.
Including the help facility of the Microsoft compilers and the
online/offline MSDN.

IOW: show some effort please.


Let me see if I understand this correctly -
1. If I ask a question that is OT I can expect to be told to use another
newsgroup,
but if I modify my question so that it is on-topic - this is the
right place.
2. If my question is on-topic I can expect to be told to RTFM as this
group isn't about helping people.

IOW: what is the magic threshold one needs to attain before one may
deign to ask a question of a learned cognoscenti such as yourself?

Mike Stenzler aka Mike Stenzler
 
A

Attila Feher

Mike said:
Let me see if I understand this correctly -
1. If I ask a question that is OT I can expect to be told to use
another newsgroup,
Yes.

but if I modify my question so that it is on-topic - this is the
right place.
Yes.

2. If my question is on-topic I can expect to be told to RTFM as this
group isn't about helping people.

No. Only if you want people to type you in a 4 chapters book explaining
std::vector.
IOW: what is the magic threshold one needs to attain before one may
deign to ask a question of a learned cognoscenti such as yourself?

First of all someone such as yourself should understand that someone such as
yourself has got all the help someone such as yourself payed for. And if
someone such as yourself stops being sarcastic and realizes that if someone
such as yourself demands help while showing up absolutely no effort of his
own so someone such as yourself starts to ask reasonable questions instead
of demanding solutions someone such as yourself will get useful answers.
Until someone such as yourself is unable to type in std::vector into his
IDE, move the cursor into it and press F1 someone such as yourself will have
somewhat tough time programming.

http://www.parashift.com/c++-faq-lite/how-to-learn-cpp.html

http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.2

http://www.sgi.com/tech/stl/Vector.html

http://www.dinkumware.com/manuals/reader.aspx?b=p/&h=vector.html

http://www.google.com/search?q=vector+tutorial+c++

http://www.google.com/search?q="stl+tutorial"
 
M

Mike Stenzler

Thanks for the example and book review.


[helpful answer snipped]
Read about containers and iterators, and get The C++ Standard
Library by Josuttis.

Thanks for the reccomendation - I have C++ Templates by Josuttis, but up
to now have relied on Stroustrup and ignored most features of the C++
std lib in favor of a mix of C std lib and MFC components - obviously
more reading is required.

[my complaint about previous poster's directive to RTFM snipped]
I suppose if you don't have the time to read your documentation, we don't
have
the time to help you.

Jonathan

now that's not even very clever - just plain rude. Thanks for the
helpful part, anyway.


Mike
 
J

Jonathan Mcdougall

IOW: show some effort please.
Let me see if I understand this correctly -
1. If I ask a question that is OT I can expect to be told to use another
newsgroup,
Yes.

but if I modify my question so that it is on-topic - this is the
right place.
Yes.

2. If my question is on-topic I can expect to be told to RTFM

Yes, if your question was already answered or if your askings are
very general. Something like "how do I use std::vector" shows
us that you don't want to look by yourself, which is not a good
thing. Get a book or something.
as this
group isn't about helping people.
Please.

IOW: what is the magic threshold one needs to attain before one may
deign to ask a question of a learned cognoscenti such as yourself?

:)

I don't know about Attila, but for the majority of the group, a question
about a specific problem directly related to standard c++ will be
answered if anyone knows the answer. A vague question will either be
ignored or answered by asking details. An ot question will either
be redirected (hopefully) or thrown by the window (as it happens
too frequently). But never, _never_ ask a question implying that
you don't have the time to do something so you're asking others.
You will almost be banned from this place.

Please, RTFM :)


Jonathan
 
M

Mike Stenzler

Attila said:
No. Only if you want people to type you in a 4 chapters book explaining
std::vector.


Does this look like 4 chapters to you? To me it looks like a simple
helpful answer.

Jonathan said:
# include <vector>
# include <iostream>

using std::vector;

class my_class
{
public:
void f()
{
std::cout << "hello" << std::endl;
}
};

int main()
{
vector<my_class> v; // a vector of my_class'es

v.push_back(my_class());
v.push_back(my_class());

// v now contains two objects

v.pop_back();

// v now contains two objects

// inserting a new object in front
v.insert(v.begin(), my_class());

v[0].f(); // "hello"
v[1].f(); // "hello"
}

IOW: what is the magic threshold one needs to attain before one may
deign to ask a question of a learned cognoscenti such as yourself?

First of all someone such as yourself should understand that someone such as
yourself has got all the help someone such as yourself payed for. And if
someone such as yourself stops being sarcastic and realizes that if someone
such as yourself demands help while showing up absolutely no effort of his
own so someone such as yourself starts to ask reasonable questions instead
of demanding solutions someone such as yourself will get useful answers.
Until someone such as yourself is unable to type in std::vector into his
IDE, move the cursor into it and press F1 someone such as yourself will have
somewhat tough time programming.


So to refine my understanding a bit further:

Off topic bad, on topic good but only if I say pretty please and provide
documentary evidence of my valiant effort and subsequent failure to
master the entire topic. Said documentation should be sufficient to show
that I have attained the required minimal knowledge one must display to
ask a question about such a grand and complex topic and then only if my
question is "reasonable". And I should be VERY careful in how I phrase
my REQUEST FOR HELP so that my humble request won't be mistaken for a
DEMAND FOR A SOLUTION.

Perhaps the following should be added to the FAQ:

Before posting the supplicant must provide the following to establish
his/her bona-fides:
1. a digital photo showing eyes red-rimmed from lack of sleep studying
the topic
2. a screen capture showing std::vector in an IDE (command line
compilers are verboten) with mouse pointer prominently displayed over
the "v"
3. a finger chopped off the right hand showing traces of yellow
highlighter
4. a credit card receipt for zero dollars paid to the great help desk in
the sky
5. a vial (or perhaps a container class) containing tears of frustration

All UNREASONABLE questions will be instantly rejected and the supplicant
suitably punished.

Mike aka Senior Vice President & Chief Technology Officer of a company
that manages (very well thankyou) close to a billion (with a B) USD - of
course I have absolutely no qualifications for the job, they just took
pity on me once they realized what a tough time I had programming.
 
M

Mike Stenzler

Jonathan Mcdougall wrote:
[snip]
Yes, if your question was already answered or if your askings are
very general. Something like "how do I use std::vector" shows
us that you don't want to look by yourself, which is not a good
thing. Get a book or something.

read my question or the whole thread or something -


please what? The group is just about people yakking at each other for
what reason? To make themselves seem smart? What?

[snip]
But never, _never_ ask a question implying that
you don't have the time to do something so you're asking others.

This is obviosly a terrible crime. Possibly a felony. Please add this
very important requirement to the FAQ so I or others like me who
actually have to do things in life won't make this terrible mistake
again.
You will almost be banned from this place.

Ahh, BANISHMENT!! Can you send a couple of guys to drag me away in a
straightjacket instead? I really like it when they twist my arms back,
and the smell of crisp white cotton...

Since I've managed to insult 2 fully qualified C++ experts on this
newsgroup by asking a simple question, I'll make it easy for you and
banish myself.
Please, RTFM :)

Yes Sir, right away sir.

Anyway, thanks for your moment of helpfulness - the code snippet you
incuded previously compiles and works and has made it clear that
std::vector is an avenue I can pursue instead of CArray. I did have a
couple of follow-on questions about the efficiency of multiple function
calls (v.push_back()) to load this vector/array and why you included
pop.back() but that obviously would be an imposition on someone such as
yourself who has only _so_much_ helpfulness to give. One must be careful
not to spoil people or they'll get lazy and keep ASKING MORE QUESTIONS!
They'll never experience the joy of spending countless hours reading and
possibly not understanding an entire complex subject of which they only
really need to know just a tiny piece of in order to get something
accomplished.

GMAFB - aka Flip Me A Bone - have you never asked someone how to do
something because you needed to know how to do it NOW and didn't have
time to learn the whole topic? I find it hard to believe you haven't.
Let me be presumptuous (see I'm obviously an untermensch - I can't even
spell and I'm too LAZY to use the damn spell checker) enough to give you
this little lesson in life - THERE'S NOTHING WRONG WITH BEING HELPFUL -
aka IT'S NICE TO BE NICE! This give a guy a fish and he eats for a
night, but teach him how to fish and he eats until he gets mercury
poisoning can be carried a bit far...

Mike
 
M

Mike Stenzler

Jonathan Mcdougall wrote:

[snip]
shows us that you don't want to look by yourself, which is not a good
[snip]

Jonathan


BTW- who is "us"? Do you speak for others?

Mike
 
W

WW

Mike said:
So to refine my understanding a bit further:

You mean to keep off-topic ranting because we did not bow and crape when you
started to demand help? OK. As you wish.
Off topic bad, on topic good but only if I say pretty please and
provide documentary evidence of my valiant effort and subsequent
failure to master the entire topic.

Stop trolling please or do not be surprised if you end up in killfiles. If
pressing F1 while your cursor is on std::vector and look at the help is too
hard for you...
Said documentation should be sufficient to show that I
have attained the required minimal knowledge one must display
to ask a question about such a grand and complex topic and then
only if my question is "reasonable". And I should be VERY
careful in how I phrase my REQUEST FOR HELP so that my
humble request won't be mistaken for a
DEMAND FOR A SOLUTION.

You have time to post that f*cking bullsh*t but you did not have time to
press F1 and look at your helpfile for vector. Congratulations. You must
be fun to work with!

[TROLLING SNIPPED]

*PLONK*
 
F

Fao, Sean

[..]
Having 2 small children and job deadline
pressures to meet, I usually don't have the luxury of poring through doco and
"teaching myself" when I'm trying to implement a technique that's new to me.
That's when I rely on asking others who have the knowledge I need - I thought
that was a big component of what Usenet was all about - people sharing
knowlege.

Well you're in luck because nobody else here has kids, jobs or deadlines to
worry about.
 
D

Default User

Mike said:
Jonathan Mcdougall wrote:

[snip]
shows us that you don't want to look by yourself, which is not a good
[snip]

Jonathan

BTW- who is "us"? Do you speak for others?


I agree with both Jonathan and WW.


You are behaving like a jackass. There are plenty of people helping
others all the time here.

You come in, don't bother lurking or checking the FAQ for topicality,
then insult the entire group by stating that they refuse to help people.
As pointed out, there are a number of tutorials regarding std::vector
and the other standard container classes.



Brian Rodenborn
 
M

Mike Stenzler

Fao said:
[..]
Having 2 small children and job deadline
pressures to meet, I usually don't have the luxury of poring through doco and
"teaching myself" when I'm trying to implement a technique that's new to me.
That's when I rely on asking others who have the knowledge I need - I thought
that was a big component of what Usenet was all about - people sharing
knowlege.

Well you're in luck because nobody else here has kids, jobs or deadlines to
worry about.

Well, I don't feel lucky... :)
 
J

Jonathan Mcdougall

2. If my question is on-topic I can expect to be told to RTFM
read my question or the whole thread or something -

FYI, I've been here since the start and I answered your questions.
Look on Google, it archives all posts, maybe you missed one. Note
that it may take up to 8h for a post to appear there.
please what?

This kind of statement ("as this group isn't about helping people.")
is useless and only makes everybody angry. Please, try to
stay polite and think about what you write.
[snip]
But never, _never_ ask a question implying that
you don't have the time to do something so you're asking others.

This is obviosly a terrible crime. Possibly a felony. Please add this
very important requirement to the FAQ so I or others like me who
actually have to do things in life won't make this terrible mistake
again.

It is.

http://www.parashift.com/c++-faq-lite/how-to-post.html

Ahh, BANISHMENT!! Can you send a couple of guys to drag me away in a
straightjacket instead? I really like it when they twist my arms back,
and the smell of crisp white cotton...

That was to be taken with a smile. I think irony does not travel well
by internet.
Since I've managed to insult 2 fully qualified C++ experts on this

Two, I don`t know, but you did not _insult_ me for sure, you just
look like a troll right now, that's it. It takes quite a big troll for
me to feel insulted.
newsgroup by asking a simple question, I'll make it easy for you and
banish myself.

By banishment, I was not talking literaly.
Yes Sir, right away sir.

That was a joke, noticed the smiley?
Anyway, thanks for your moment of helpfulness - the code snippet you
incuded previously compiles and works and has made it clear that
std::vector is an avenue I can pursue instead of CArray. I did have a
couple of follow-on questions about the efficiency of multiple function
calls (v.push_back()) to load this vector/array and why you included
pop.back()

Well go for it and stop saying these things!! This discussion is *useless*,
people are here to help other people, not to talk like that. So please,
and this applies to *everyone* including me, shut the **** up, let's
forget that thread, and let's start a new one about std::vectors, ok ?
I'm getting tired of that shit.

<snipped>


Jonathan
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top