Sponsored Content
Top Forums Programming passing object to function, columns class Post 302513271 by Corona688 on Tuesday 12th of April 2011 06:00:57 PM
Old 04-12-2011
Quote:
Originally Posted by LMHmedchem
I need to load tabular data from a text file. Reading the data into a string for each row, and then parsing the row by the delimiter is the only way I know how to do that at the moment.
Sure, but why pass it as a stringstream? Why not just pass it as a string?

Or if you really want to pass it a stringstream, why not make one function to handle file streams and stringstreams? The whole point of a stream is you don't have to care what kind it is...
Quote:
I know you can convert from string to real, etc, but that seams like a silly solution.
Oh, I see. You want a stringstream so that you can stream>>vartype with it.

It's a choice between
Code:
if(vartype==1) stream>>vartype1;
else if(vartype==2) stream>>vartype2;
...

and
Code:
if(vartype==1) sscanf(string, "%f", &vartype1);
else if(vartype==2) sscanf(string, "%d", &vartype2);

Be honest with yourself: There's no way around sheer brute force either way, they're equally ugly. >> looks prettier but is harder to check for errors and more difficult to process formatted input with.

You can indeed pass a string stream pointer that way, by the way, but you have to use its members with -> instead of .



Another concept that may help you is the union, with which you can build one class to hold many types.

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

class multitype
{
public:
        multitype(const char *i):e(strdup(i)),type(T_CSTRING)   { }
        multitype(float i):c(i),type(T_FLOAT)   { }
        multitype(char i):b(i),type(T_CHAR)             { }
        multitype(int i):a(i),type(T_INT)               { }

        inline operator int() const     { return(a); }
        inline operator float() const   { return(c); }
        inline operator char() const    { return(b); }
        inline operator const char *() const    { return(e); }

        ~multitype()
        {       if(type==T_CSTRING) free(e);            }

protected:
        enum { T_INT, T_CHAR, T_FLOAT, T_DOUBLE, T_CSTRING } type;

        // All members of a union occupy the SAME memory.
        // You can't use more than one at once.
        union
        {
                int a;
                char b;
                float c;
                char *e;
                // You cannot put classes in a union though, since it won't know
                // which constructor to use!
        };
};

int main(int argc, char *argv[])
{
        multitype str("asdf"), f(3.14159f), i(32);

        printf("string %s float %f int %d\n", (const char *)str,
                (float)f, (int)i);
        return(0);
}


Last edited by Corona688; 04-12-2011 at 07:18 PM..
 

10 More Discussions You Might Find Interesting

1. Programming

Listing function exports from object file

Is it possible to view all the functions exported by a given object file? "dump -tv" comes the closest, but what exactly am I looking for to determine whether the symbol exists in the object file? Essentially, I have a library that requires a call to "xdr_sizeof" and the compile is failing... (5 Replies)
Discussion started by: DreamWarrior
5 Replies

2. Programming

How to make a function friend to both base and derived class

Hi, I have a base class and derived a class from the base class, i want to print & read the data for the object created for the derived class,so i have overloaded both the << and >> operators and also have done the foward declaration. Below is the code snippet, #include <iostream> class... (3 Replies)
Discussion started by: ennstate
3 Replies

3. Shell Programming and Scripting

Passing global variable to a function which is called by another function

Hi , I have three funcions f1, f2 and f3 . f1 calls f2 and f2 calls f3 . I have a global variable "period" which i want to pass to f3 . Can i pass the variable directly in the definition of f3 ? Pls help . sars (4 Replies)
Discussion started by: sars
4 Replies

4. Programming

Handling a signal with a class member function

Hello, i am using the sigaction function to handle the SIGCHLD signal.Is it possible to use a class member function as the handler function (the sa_handler member of the sigaction structure)? The function's signature is: void (*sa_handler)(int);so i don't think i can use a static member function... (2 Replies)
Discussion started by: Zipi
2 Replies

5. Shell Programming and Scripting

How to Call external function in .C or .So (Shared Object)

Hi, Anybody know any way to Call with Shell Script an external function wrote in .C or .So (Shared Object) on AIX enviroment and returning parameters of .C or .SO to Shell Script? Tks!! (6 Replies)
Discussion started by: rdgsantos
6 Replies

6. Programming

question about function object

I have a code as following: #include <iostream> #include <algorithm> #include <list> using namespace std; //the class Nth is a predicates class Nth{ private: int nth; int count; public: Nth(int n):nth(n),count(0){} bool operator()(int){ ... (2 Replies)
Discussion started by: homeboy
2 Replies

7. UNIX and Linux Applications

opends- help with custom object class

we have 2.2.0 of opends running on RedHat 2.6.21 and we're trying to setup a structure that will suit our needs. One of the things we'd like to do is create our own custom object classes based off some of the existing ones you get out of the box. The opends documentation covers this here (sorry, it... (1 Reply)
Discussion started by: snafu
1 Replies

8. Programming

Created a wrapper for a function in a class.

I have a class called Parsing with the following function. I want to create a wrapper for it, so that I call it using GetReal rather than GetFloat. Bit confused on how to do this. class Parsing { private: int Length; // int Ptr; ... (3 Replies)
Discussion started by: kristinu
3 Replies

9. Programming

How to initialize an object with another object of different class?

How to initialize an object of class say "A", with an object of type say "B". The following code give the error message "error: conversion from âAâ to non-scalar type âBâ requested" #include <iostream> using namespace std; class B; class A{ public: A() { cout <<"\nA()" << endl; } ... (1 Reply)
Discussion started by: techmonk
1 Replies

10. Programming

C++ : Base class member function not accessible from derived class

Hello All, I am a learner in C++. I was testing my inheritance knowledge with following piece of code. #include <iostream> using namespace std; class base { public : void display() { cout << "In base display()" << endl; } void display(int k) {... (2 Replies)
Discussion started by: anand.shah
2 Replies
All times are GMT -4. The time now is 09:55 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy