What are Foo and Bar?
- null_pointer
Sabre Multimedia
Weird C++ type conversion problem (HARD)...
quote: Original post by null_pointer
What are Foo and Bar?
Metasyntactic variables. Essentially Foo == something, Bar == something else.
Check out this link for more (warning: big html page, takes a while load).
Vlad, what I was referring to when I said "there can''t possibly be a use for something that complicated" was the code in the original post. I''m not going to go into the merits of C++ vs. not C++.
And as far as a having a new programmer having any business being a programmer, I have to disagree with you. There is a very stark line between a programmer who is capable of learning and gaining experience and one who already _has_. Many many bright programmers in this industry start out being complete novices in terms of practical game development knowledge. I would say probably 99% of the people in this forum would fall in this category, prior non-game programming experience notwithstanding. It takes more than a few chapters in the currently popular C++ manual to teach when using some particular feature is a good idea or not. Every C++ book I''ve ever seen always seems to lead people to try and use it _all_ at once, without stopping to learn the implications of it. But at the same time, you have nothing but deadlines and tight schedules in game development. You cannot afford to fall months behind because some system is horribly impractical.
My point is that for _game_ development purposes, I have personally found that C++ is more often a hindrance than a bonus. If we can keep this discussion in the realm of _game_ development, my argument becomes more clear.
And as far as a having a new programmer having any business being a programmer, I have to disagree with you. There is a very stark line between a programmer who is capable of learning and gaining experience and one who already _has_. Many many bright programmers in this industry start out being complete novices in terms of practical game development knowledge. I would say probably 99% of the people in this forum would fall in this category, prior non-game programming experience notwithstanding. It takes more than a few chapters in the currently popular C++ manual to teach when using some particular feature is a good idea or not. Every C++ book I''ve ever seen always seems to lead people to try and use it _all_ at once, without stopping to learn the implications of it. But at the same time, you have nothing but deadlines and tight schedules in game development. You cannot afford to fall months behind because some system is horribly impractical.
My point is that for _game_ development purposes, I have personally found that C++ is more often a hindrance than a bonus. If we can keep this discussion in the realm of _game_ development, my argument becomes more clear.
Volition, Inc.
To daveb:
OK, I better clarify what I said.
By that remark I mean professional developers, game or non-game. That''s what I thought you meant by "a 1 or 2 year guy" - a person with 1 or 2 years professional experience. I was trying to say that in professional software development new technologies should not be discarded solely because Joe R. Programmer''s eyes are going to cross. Ideally, all implications of using new technology should be considered, risks weighted, blah-blah-blah…
I don''t quite get were you disagree with me. I was saying that a capability to learn is the most important ability a programmer can have. You talk about experience vs. capability to learn. You actually seem to agree with me by saying that many bright game programmers started out as novices. Care to clarify?
Exactly. Knowledge gained from books is abstract, practical knowledge is what gets projects done on time and under budget. When doing professional development one rarely gets a chance to play with new stuff and gain that valuable practical knowledge. For a hobbyist all of the deadlines are self-imposed. There’s no money or reputation at stake. One can (and should) afford to try out new things and see if they work or not. This is the prefect time to learn the implications of different technologies, their advantages and disadvantages. And trying out a refcounted smart pointer to manage memory deallocation is part of the learning process.
You are able to form a conclusion that C++ is ill suited for game development because you’ve had experience with it and saw it fail. People on this board need their own experience to make their own educated decisions. Maybe DireWolf will agree with your conclusion after he tries out his smart pointer, maybe he won’t because he does not have to port to consoles nor does he have to deal with team members who go cross-eyed when they see C++ code – that’s not the point. Point is he’ll never understand the implications of using templates if he does not try.
That’s why I disagree with your “can’t possibly be of use” remark, Dave. Oh, and thanks for sharing horror stories about C++ on consoles.
OK, I better clarify what I said.
quote: Original post by daveb
And as far as a having a new programmer having any business being a programmer, I have to disagree with you. There is a very stark line between a programmer who is capable of learning and gaining experience and one who already _has_. Many many bright programmers in this industry start out being complete novices in terms of practical game development knowledge. I would say probably 99% of the people in this forum would fall in this category, prior non-game programming experience notwithstanding.
By that remark I mean professional developers, game or non-game. That''s what I thought you meant by "a 1 or 2 year guy" - a person with 1 or 2 years professional experience. I was trying to say that in professional software development new technologies should not be discarded solely because Joe R. Programmer''s eyes are going to cross. Ideally, all implications of using new technology should be considered, risks weighted, blah-blah-blah…
I don''t quite get were you disagree with me. I was saying that a capability to learn is the most important ability a programmer can have. You talk about experience vs. capability to learn. You actually seem to agree with me by saying that many bright game programmers started out as novices. Care to clarify?
quote:
It takes more than a few chapters in the currently popular C++ manual to teach when using some particular feature is a good idea or not. Every C++ book I''ve ever seen always seems to lead people to try and use it _all_ at once, without stopping to learn the implications of it. But at the same time, you have nothing but deadlines and tight schedules in game development. You cannot afford to fall months behind because some system is horribly impractical.
My point is that for _game_ development purposes, I have personally found that C++ is more often a hindrance than a bonus. If we can keep this discussion in the realm of _game_ development, my argument becomes more clear.
Exactly. Knowledge gained from books is abstract, practical knowledge is what gets projects done on time and under budget. When doing professional development one rarely gets a chance to play with new stuff and gain that valuable practical knowledge. For a hobbyist all of the deadlines are self-imposed. There’s no money or reputation at stake. One can (and should) afford to try out new things and see if they work or not. This is the prefect time to learn the implications of different technologies, their advantages and disadvantages. And trying out a refcounted smart pointer to manage memory deallocation is part of the learning process.
You are able to form a conclusion that C++ is ill suited for game development because you’ve had experience with it and saw it fail. People on this board need their own experience to make their own educated decisions. Maybe DireWolf will agree with your conclusion after he tries out his smart pointer, maybe he won’t because he does not have to port to consoles nor does he have to deal with team members who go cross-eyed when they see C++ code – that’s not the point. Point is he’ll never understand the implications of using templates if he does not try.
That’s why I disagree with your “can’t possibly be of use” remark, Dave. Oh, and thanks for sharing horror stories about C++ on consoles.
quote: Original post by daveb
Every C++ book I''ve ever seen always seems to lead people to try and use it _all_ at once, without stopping to learn the implications of it. But at the same time, you have nothing but deadlines and tight schedules in game development. You cannot afford to fall months behind because some system is horribly impractical.
You know those examples regarding operator overloading and string classes? You may blow your top, but there are TONS of books that teach practical information about those things. I can name at least one:
C++ for Dummies 3rd Edition, written by Randy Davis, published by IDG Books Worldwide, Inc.
A little 400-page (little as computer books go) book taught me most of what I know - and I must say I haven''t found the book to be wrong yet. If you can read between the red in your eyes, it''s a very practical book. In fact, it''s made for advanced C programmers who are making the move to C++. I don''t know of any books like the ones you are describing, so I can''t comment on that. Besides, if you search the web and great programming sites like this one, I''m sure you''d find quite a few that address very advanced performance issues in C++. Free.
I think the problem is that C programmers try to walk into C++ expecting to "just know it," without stopping to pick up reference or learning materials on language. C++ is an excellent language. It''s not perfect, but it has a very solid foundation (C-like syntax), and it is, as far as I know, the lowest high-level language, which favors performance over encapsulation when it comes to language features. You get to pick and choose which features and what level suit each problem. Want garbage collection? Implement it transparently, if you like. etc. That''s why you''ve still got pointers, stack-based variables, dynamic arrays without bounds-checking, etc.
I think that I know it almost like the back of my hand - at least to the point where there aren''t any "gotchas" to doing something, and I can probably tell you what a piece of C++ code is doing. (In fact, many of the people here spend their time fixing problems in tons of code, so we''re not exactly without experience in C++ here.) I''d ask you to place a problem before me, and I''ll try to model it, but I am still working on my first game. On the other hand, it would be just as silly to find a person with very little experience in C++ and compare him to an expert in C, wouldn''t it?
And vice versa - let''s look at a common C gotcha (just to show how silly these examples are to anyone who knows the language):
typedef struct _tagREALLYBIGSTRUCT
{
char name[80];
byte data[431];
byte data_flags;
} REALLYBIGSTRUCT; // 512 bytes!
void some_function_that_is_called_often(REALLYBIGSTRUCT rbs) // gotcha!
{
/* code */
}
"But the book said it was good to put things in structs!" (<-- example of a really stupid argument - I don''t think the book said to do this!)
IMHO, writing software in C++ requires two things: knowledge of the problem domain and knowledge of the language. Most problems come from a lack of one or both. You certainly can''t run a C++ game company with programmers that have < 1 year of experience any more than you could run a C game company with programmers that have < 1 year of experience in C. (Well, not a professional company with deadlines, and press releases, and porting issues, etc.)
C experience != C++ experience
- null_pointer
Sabre Multimedia
Bottom line : you''re doing a great disservice to all the new programmers in this group by refusing to see outside of the C++ box. There''s just no argument. They''re going to have more problems learning and experimenting with hardcore C++ than if they used a simple blend of C/C++. Many of them will never be able to complete a cool idea/project they had because they get mired in deep C++. You''re shoving the "C++ is everything and if you can''t understand that, you''re obviously an idiot" mentality down their throats.
I''m outta here
I''m outta here
Volition, Inc.
OK you two break it up
I would like to get back to the real purpose of this topic - which is helping me finish my smart pointer class.
UPDATE:
I have figured out a way to work around VC++ 6.0's inability to deal with member template functions properly (try having a function that takes two template parameters as a friend of a class - VC++ doesn't like it). My solution uses a external template friend function to assign a smartptr to a smartptr WITHOUT using public data members (therefore count and ptr are still private).
Here is the implementation (hopefully this board doesn't gobble up some of the characters)
Please remember that I haven't really given the class a thorough look-over. I'm sure there are useful member functions that could be added or what not but that isn't the point. What I AM looking for is constructive criticism and ways to optimize this class.
TEMPLATE CLASS: SMARTPTR
I need help to find:
- bugs
- memory leaks
- a better way to implement this class
Another question is why is my copy constructor never called when I pass a smartptr to a function?
Thanks for the help guys.
- Dire Wolf
direwolf@digitalfiends.com
Edited by - Dire.Wolf on September 7, 2000 11:56:44 AM
I would like to get back to the real purpose of this topic - which is helping me finish my smart pointer class.
UPDATE:
I have figured out a way to work around VC++ 6.0's inability to deal with member template functions properly (try having a function that takes two template parameters as a friend of a class - VC++ doesn't like it). My solution uses a external template friend function to assign a smartptr to a smartptr WITHOUT using public data members (therefore count and ptr are still private).
Here is the implementation (hopefully this board doesn't gobble up some of the characters)
Please remember that I haven't really given the class a thorough look-over. I'm sure there are useful member functions that could be added or what not but that isn't the point. What I AM looking for is constructive criticism and ways to optimize this class.
TEMPLATE CLASS: SMARTPTR
template<class T>class smartptr{ public: typedef T element_type; template<class T, class T2> friend void assign_smartptr(long** count, void** dest, smartptr<T2>* src); explicit smartptr(T* p) : ptr(p), count(new long(1)) { } template<class T2> smartptr(const smartptr<T2>& rhs) : ptr(rhs.ptr), count(rhs.count) { ++(*count); } template<class T2> smartptr<T>& operator =(T2* rhs) { _free(); ptr = rhs; count = new long(1); return *this; } template<class T2> smartptr<T>& operator =(smartptr<T2>& rhs) { _free(); // provide compile time type checking (looks ugly but it works) T2 *t2 = 0; T *t1 = t2; assign_smartptr<T, T2>(&count, reinterpret_cast<void**>(&ptr), &rhs); return *this; } ~smartptr() { _free(); } T& operator *() { return *ptr; } T* operator->() { return static_cast<T*>(ptr); } long refcount() const { return *count; } void release() { _free(); } private: void _free() { if(--(*count) == 0) { delete ptr; delete count; ptr = 0; count = 0; } } T *ptr; long *count;};template<class T, class T2>inline void assign_smartptr(long** count, void** dest, smartptr<T2>* src){ *dest = src->ptr; *count = src->count; ++(**count);}[/source]<b>DRIVER PROGRAM </b> [source]#include "smart_ptr.h"#include <iostream>// function prototypesvoid print_refcount(const char* message);template<class T> void Foo(smartptr<T> ptr);// we'll be using std::cout and std::endlusing std::cout;using std::endl;// declare classes to be used for testingclass Animal{ public: Animal() {} virtual ~Animal() {} virtual void Speak() const { cout << "I'm an animal!"; }};class Dog : public Animal{ public: Dog() {} virtual ~Dog() {} virtual void Speak() const { cout << "I'm a dog!"; }};class BigDog : public Dog{ public: BigDog() {} virtual ~BigDog() {} virtual void Speak() const { cout << "I'm a big dog!"; }};class Cat : public Animal { public: Cat(); virtual ~Cat() {} virtual void Speak() const { cout << "Cat Speak" << endl; }};// global objects of test classes (global due to print_refcount function)smartptr<Animal> myanimal(new Animal);smartptr<Dog> mydog(new Dog); smartptr<BigDog> mybigdog(new BigDog);int main(){ // show reference count before any assignments are made print_refcount("Before any assignments..."); // assignment of mydog to myanimal myanimal = mydog; print_refcount("After myanimal = mydog: "); // assignment of mybigdog to myanimal myanimal = mybigdog; print_refcount("After myanimal = mybigdog: "); // assignment of mybigdog to mydog mydog = mybigdog; print_refcount("After mydog = mybigdog: "); // lets make myanimal an Animal again myanimal = new Animal; print_refcount("After myanimal = new Animal"); // see if we can pass a smartptr to a function Foo(myanimal); return 0;}void print_refcount(const char* message){ using std::cout; using std::endl; cout << message << endl; cout << "----------------" << endl; cout << " MyAnimal reference count of: " << myanimal.refcount() << " and says, \""; myanimal->Speak(); cout << endl; cout << " MyDog reference count of: " << mydog.refcount() << " and says, \""; mydog->Speak(); cout << endl; cout << " MyBigDog reference count of: " << mybigdog.refcount() << " and says, \""; mybigdog->Speak(); cout << endl << endl;}template<class T>inline void Foo(smartptr<T> ptr){ using std::cout; using std::endl; cout << "Foo test, RefCount: " << ptr.refcount() << " Speak: "; ptr->Speak(); cout << endl;}
I need help to find:
- bugs
- memory leaks
- a better way to implement this class
Another question is why is my copy constructor never called when I pass a smartptr to a function?
Thanks for the help guys.
- Dire Wolf
direwolf@digitalfiends.com
Edited by - Dire.Wolf on September 7, 2000 11:56:44 AM
[email=direwolf@digitalfiends.com]Dire Wolf[/email]
www.digitalfiends.com
www.digitalfiends.com
quote:
Another question is why is my copy constructor never called when I pass a smartptr to a function?
That''s because you''ve only defined a template copy ctor, a template copy constructor doesn''t stop the compiler generating one. So if both smart pointers are the same time the compiler generated copy constructor will be called. Which means that both pointers with point to the same data, but the ref count will be wrong. To fix it you need to define a none-template copy constructor. You might want to take a look at boost to get some ideas, they have a nice collection of smart pointers (as well as a load of other stl compatible stuff).
Yeah I just noticed that I forgot to make the copy constructor non-templated I also forgot to check for self-assignment in both operator= but I fixed that quickly
So far everything seems to be working OK with the class.
I''m going to post the final source code soon and I would appreciate if you guys could try compiling it with other compilers and on other platforms.
Thanks again,
- Dire Wolf
direwolf@digitalfiends.com
So far everything seems to be working OK with the class.
I''m going to post the final source code soon and I would appreciate if you guys could try compiling it with other compilers and on other platforms.
Thanks again,
- Dire Wolf
direwolf@digitalfiends.com
[email=direwolf@digitalfiends.com]Dire Wolf[/email]
www.digitalfiends.com
www.digitalfiends.com
BTW I did look at Boost a few days ago and they are using public data members for the reference count and internal data pointer - I was trying to avoid that. Other than that their smart pointers are nice and a source of good ideas
- Dire Wolf
direwolf@digitalfiends.com
- Dire Wolf
direwolf@digitalfiends.com
[email=direwolf@digitalfiends.com]Dire Wolf[/email]
www.digitalfiends.com
www.digitalfiends.com
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement