Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

How do I pass argv to a class

Posted on 2001-06-20
4
483 Views
Last Modified: 2010-04-02
I want to be able to pass the argv parameter given within the main() function of an app to a class, and assign it to a class member, such as :-

// from main

main(int argc, char* argv[])
{
   CMyClass myClass;

   myClass.Init(argc, argv);

......

}

// in myClass

CMyClass::Init(int argc, char* argv[])
{
   m_nArgc = argc;

  m_szArgv = argv;     // is this correct;

}

What I would like to know, is what data type must m_szArgv be declared as and how should argv be assigned to it?
0
Comment
Question by:carlpaddick
  • 2
4 Comments
 
LVL 8

Accepted Solution

by:
mnashadka earned 50 total points
ID: 6209896
m_szArgv should be a char **, and the way you assign it is fine.  If you don't want to mess with char **'s, you might want to look at assigning argv to a vector of strings, like:

class CMyClass
{
...
  std::vector<std::string> m_Argv; // Could also be CString
};

// in myClass

CMyClass::Init(int argc, char* argv[])
{
  for(int i = 0; i < argc; ++i)
    m_Argv.push_back(argv[i]);
}
And you can still use the subscript operator to get data out later.  e.g. std::string param1 = m_Argv[0];

I hope this helps you.
0
 

Author Comment

by:carlpaddick
ID: 6209938
Thanks mnashadka,

Good answer.  Please have the points.

Keep up the good work.
0
 
LVL 22

Expert Comment

by:nietod
ID: 6222886
Does anyone know if this accepted answer was posted as a comment or as an answer?
0
 

Author Comment

by:carlpaddick
ID: 6223717
nietod,

Again, I posted this comment as an accepted answer.

Thanks,

Carl
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question