473,387 Members | 1,882 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Pointers vs. Static Objects

Hi All,

Could anyone tell me when one should use objects and when pointers?

Thanks,
Greg

Aug 28 '07 #1
6 1594
greg wrote:
Could anyone tell me when one should use objects and when pointers?
FAQ 8.6 answers a similar question. Did you read it?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 28 '07 #2
On 2007-08-29 00:00, greg wrote:
Hi All,

Could anyone tell me when one should use objects and when pointers?
Pointers and objects are kind of orthogonal concepts, since pointers
points to objects (static or not), so you can't really have pointers
without objects (if you consider the built-in types to be objects).

Are you perhaps referring to when to pass parameters as pointers or when
to pass by value. My answer should be, use references when you can, by
value where it makes sense and pointers only when you have to.
Pointers are very powerful but they are also the source of many errors
and much can be done without them in safer ways, which is generally
preferable. Learn the differences between these methods and it's often
quite clear which is the best to use.

--
Erik Wikström
Aug 28 '07 #3
So I have had an application that I have been using for over 5 years
developed under Gnu C++ 2.96 on fedora core 2. I recently updated to
fedora core 6 and now this same application is breaking.

Here is the typical message:
/home/joec/develop/c_source/SMARTrain_cmd/main.cpp:139: undefined
reference to `TSettings::TSettings()'
/home/joec/develop/c_source/SMARTrain_cmd/main.cpp:140: undefined
reference to `TRider::TRider()'
/home/joec/develop/c_source/SMARTrain_cmd/main.cpp:146: undefined
reference to `TBike::init_bike(TBike**)'
/home/joec/develop/c_source/SMARTrain_cmd/main.cpp:147: undefined
reference to `TDaily::init_date(TDaily**)'
/home/joec/develop/c_source/SMARTrain_cmd/main.cpp:148: undefined
reference to `TMaint::init_maint(TMaint**)'

Here is a simple class declaration:
/*-------------------------------------------------------------------*/
class TSettings {
private:

public:
char vstr[2];
char date_fmt[2];
char dist_fmt[2];
char alm_set[2];
char hrm_set[2];
char upd_on[2];
char fil_sve[2];

TSettings();
void Display_settings();
TSettings* System_set(TSettings *uset, bool file);
TSettings* Edit_settings(TSettings *uset, char fmt[4]);

};
/*----------------------------------------------------------------------------------*/
class TBike {
private:
public:
char Type[2 * BYTE]; // This is the keyword entry for this
linked list.
char Bike[2 * BYTE];
char Wheels[4 * BYTE];
char Grouppo[3 * BYTE];
bool track;
TBike* nxt_Bike;

TBike* init_bike(TBike **pBike);
TBike* bike_profile(TBike **pBike);
void print_bike(TBike *pBike);
int cnt_bike_list(TBike *pBike);
};
/*----------------------------------------------------------------------------------*/

Here are where the above classes are first declared in main.cpp:

TSettings uset;
TRider rider;

I heard that changes were made for the new(er) version of g++ (FC6 uses
gcc version 4.1.1), but I didnt think that it would break my code as it
has.

A little help, please...

You can reply directly to my email if you wish to call me a noron for
not updating sooner.

J. Cipale

Aug 29 '07 #4
On Aug 29, 8:27 am, joe cipale <j...@aracnet.comwrote:
So I have had an application that I have been using for over 5 years
developed under Gnu C++ 2.96 on fedora core 2. I recently updated to
fedora core 6 and now this same application is breaking.

Here is the typical message:
/home/joec/develop/c_source/SMARTrain_cmd/main.cpp:139: undefined
reference to `TSettings::TSettings()'
/home/joec/develop/c_source/SMARTrain_cmd/main.cpp:140: undefined
reference to `TRider::TRider()'
/home/joec/develop/c_source/SMARTrain_cmd/main.cpp:146: undefined
reference to `TBike::init_bike(TBike**)'
/home/joec/develop/c_source/SMARTrain_cmd/main.cpp:147: undefined
reference to `TDaily::init_date(TDaily**)'
/home/joec/develop/c_source/SMARTrain_cmd/main.cpp:148: undefined
reference to `TMaint::init_maint(TMaint**)'

Here is a simple class declaration:
/*-------------------------------------------------------------------*/
class TSettings {
private:

public:
char vstr[2];
char date_fmt[2];
char dist_fmt[2];
char alm_set[2];
char hrm_set[2];
char upd_on[2];
char fil_sve[2];

TSettings();
void Display_settings();
TSettings* System_set(TSettings *uset, bool file);
TSettings* Edit_settings(TSettings *uset, char fmt[4]);

};

/*-------------------------------------------------------------------------*---------*/
class TBike {
private:
public:
char Type[2 * BYTE]; // This is the keyword entry for this
linked list.
char Bike[2 * BYTE];
char Wheels[4 * BYTE];
char Grouppo[3 * BYTE];
bool track;
TBike* nxt_Bike;

TBike* init_bike(TBike **pBike);
TBike* bike_profile(TBike **pBike);
void print_bike(TBike *pBike);
int cnt_bike_list(TBike *pBike);};

/*-------------------------------------------------------------------------*---------*/

Here are where the above classes are first declared in main.cpp:

TSettings uset;
TRider rider;

I heard that changes were made for the new(er) version of g++ (FC6 uses
gcc version 4.1.1), but I didnt think that it would break my code as it
has.
The error messages are pretty clear and easy to understand: undefined
reference to so-and-so function. In simpler language, this means that
the function definition is not found.

As an aside, I doubt if the compiler proper would give such messages.
The kind of error messages you have quoted is a linker's job.
-N

Aug 29 '07 #5
I guess my question was: how do we know when to allocate stuff on the
heap and when on the stack?

Thanks,
Greg

Aug 29 '07 #6
On 2007-08-29 06:38, greg wrote:
I guess my question was: how do we know when to allocate stuff on the
heap and when on the stack?
The differences in lifetime for objects on the heap vs. those on the
stack (automatic storage) usually makes the choice easy, if you only
need the object as long as the current function, or any function called
by it, is running then automatic storage is usually the way to go. If
you need the object for a longer period of time then you probably should
use the heap. An exception might be when you need lots of objects or
have really large objects, these are usually placed on the heap.

Learn about the differences and how they are affected by scope and what
other obligations that comes with them and you'll have little problem
deciding which to use.

--
Erik Wikström
Aug 29 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Peter B. Steiger | last post by:
The latest project in my ongoing quest to evolve my brain from Pascal to C is a simple word game that involves stringing together random lists of words. In the Pascal version the whole array was...
79
by: Me | last post by:
Just a question/observation out of frustration. I read in depth the book by Peter Van Der Linden entitled "Expert C Programming" (Deep C Secrets). In particular the chapters entitled: 4: The...
9
by: Timothee Groleau | last post by:
Hi all, My name is Tim, I'm just getting started with C++ and this is my first post to the group. Is there a standard recommended approach to use a vector of pointers along with <algorithm>?...
7
by: zombek | last post by:
Hi. I'd like to know which is faster and when to use: this (int type is just an example I mean it generally): int* ptr = new int (123); int* arr = new int ; and this: int num = 123; int...
7
by: Erdal Mutlu | last post by:
Hi, I am trying to design a base class (interface) with two or more subclasses as follows: class A { .... virtual static A* getByName(const string x)=0 const; }
6
by: Jia | last post by:
Hi all, I have a class foo which has a static vector of pointers of type base class, and a static function to set this vector. #include <iostream> #include <vector> using namespace std;...
11
by: desktop | last post by:
I have made this example: #include<iostream> class Beer { public: Beer() { std::cout << "made a beer\n"; num = 1; }
6
by: parag_paul | last post by:
Suppose I have a struct typedef struct atype{ int a; int b; } at; main(){ static at* j= 0;
1
by: FNA access | last post by:
Hello all, I am somewhat new to C++, so please forgive my ignorance. I have an assignment that I am working on and while I do not want someone to do my homework for me, some advice to point me in...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.