473,387 Members | 1,904 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.

Class as an argument and overloading

Just a quick question:

1) Is it possible to pass the name of a class or struct as an argument ?
ex.
void function(class muffin)
{
muffin * y = new muffin;
}

you get the idea

2) How do I overload, or whatever, a class so that it can receive values from functions like scanf? How does the string class do this?

Thanks in advance!
Sep 26 '07 #1
4 1229
RRick
463 Expert 256MB
You're close, but in C++ keywords like class and struct are usually used only in the declaration of the class or structure. You need to also pass a name of the object for muffin (i.e. y) in the parameter list.

I assume you want the value created inside the function to be used outside the function. In this case, a C style solution is:
Expand|Select|Wrap|Line Numbers
  1. void function( muffin **y)
  2. {
  3.     * y = new muffin;
  4. }
A C++ style solution would be
Expand|Select|Wrap|Line Numbers
  1. void function( muffin *& y)
  2.  {
  3.     y = new muffin;
  4.  }
Since y is not used inside the method, you can have the function simply return the value of the pointer. This way, you don't have to worry about passing a parameter.
Expand|Select|Wrap|Line Numbers
  1. muffin *  function( void)
  2.  {
  3.     muffin * y = new muffin;
  4.     return y;
  5.  }
Sep 26 '07 #2
That's not quite what i meant...

I mean can you do something like this:

Expand|Select|Wrap|Line Numbers
  1. Class class1
  2. {
  3.    ...
  4. };
  5.  
  6. void make_class(class chose)
  7. {
  8.    chose * x = new chose;
  9. }
  10.  
  11. int main()
  12. {
  13.    make_class(class1);
  14.    ...
  15. }
and how about my second question?
Sep 27 '07 #3
durand
1
Just a quick question:

1) Is it possible to pass the name of a class or struct as an argument ?
ex.
void function(class muffin)
{
muffin * y = new muffin;
}

you get the idea

2) How do I overload, or whatever, a class so that it can receive values from functions like scanf? How does the string class do this?

Thanks in advance!
1)
Use templates instead.

// Base function
template <class T> // T = muffin
void function(T dummy) // dummy: any value of the required type
{
T * y = new T;
}

// specialization
template <> // T = muffin
void function<>(int dummy)
{
int * y = new int;
}

2) I did not understand very well but use function pointers to pass functions as arguments.
Sep 27 '07 #4
RRick
463 Expert 256MB
Durand is correct. Templates are the way to go here. You can't just declare the class in a parameter list because that defines a specific method with a specific value being passed.

As for your second question, I'm not sure what you want. Scanf "returns" an int. When you say you want to "receive values from scanf", all you're going to get it is an int saying how many things were parsed.

Scanf also takes accepts pointers to strings and other primitives and then sets them. Is this what you're talking about? In this case, you pass a pointer to the subroutine of what you want modified and the subroutine makes the changes.

Finally, scanf also takes what is called a variable length arguement list. This allows you to set the format and then add any number of variables.
Sep 27 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

50
by: Dan Perl | last post by:
There is something with initializing mutable class attributes that I am struggling with. I'll use an example to explain: class Father: attr1=None # this is OK attr2= # this is wrong...
7
by: Doran_Dermot | last post by:
Hi All, I've seen lots of code in which the attributes of a class are accessed and modified using two separate methods. For example: class Problems: def __init__( self, refNum ):...
24
by: Brad Marts | last post by:
I would like to have a function that takes as an argument a base class but performs differently depending on which type of derived class is passed. Can I tell which derived class is passed? For...
9
by: Jon Wilson | last post by:
I have a class which needs to accumulate data. The way we get this data is by calling a member function which returns float on a number of different objects of different type (they are all the...
2
by: franklini | last post by:
hello people i. can anybody help me, i dont know what is wrong with this class. it has something to do with the me trying to override the input output stream. if i dont override it, it works fine....
1
by: Jesper | last post by:
Hi, Are there any serious drawbacks using a class instead of struct to implementing your own type. I a textbook of mine on C# the author recommend operator overloading on value types only....
5
by: Chris | last post by:
Hi, I don't get the difference between a struct and a class ! ok, I know that a struct is a value type, the other a reference type, I understand the technical differences between both, but...
9
by: Brian Henry | last post by:
If i inherite a queue class into my class, and do an override of the enqueue member, how would i then go about actually doing an enqueue of an item? I am a little confused on this one... does over...
2
by: Armond VanHeusen | last post by:
A quick question if I may (this relates to .NET1.1 by the way): If I have a C# class defined in my project that does operator overloading, can I use this in my VB code? For example, I have a...
7
by: quarup | last post by:
I want to specialize a template function that lives inside a class, but am getting a compile error in VS.net 2003. Here's my code: template <class T> class A { public: template <class U> void...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.