zoukankan      html  css  js  c++  java
  • Placement of class definition and prototype

    When I create a function, I can put the code for it after main if I put the prototype above main. For example,

    int myFunction(int a)
    {
    return(a); 
    }

    would have the prototype..

    int myFunction(int a);

    above main.

    However, I have not been able to get this to work for a class definition.

    If I put …

    class myClass
    {
    
    };

    below main,

    I get an error if I put

    class myClass;

    above main. The error occurs where the class is used within main, and the error is "unknown type name." That's with the c++ compiler that is part of Xcode.

    What kind of prototype should I enter above main if the class definition is below main?



    When you call a function and the definition is not available, the compiler doesn't have to know the contents in order to continue evaluating the rest of the code (eg: stack usage at the call site). It only needs to know the function's signature to ensure the correct parameters are going to be passed into the function. The linker will hook up the actual address for the function call after the compiler is done.

    However when you are using a class it has to know about the details of it - not just that it exists - because it'll need to ensure proper layout on the stack, what parameters are required for the constructor, etc.

    The details of the class' functions are of course like regular functions - it just needs the signature to work with - they can be defined later.


  • 相关阅读:
    notepad++插件 small
    js中面向对象 small
    #九阴真经#优选配置渲染流程简要分析[Flexi引擎]
    gkENGINE HDR流程简析
    新的开始
    以前的一些画
    gkENGINE 开发两年半总结(上)
    gkENGINE跨平台的问题总结
    win7下安装xp双系统
    Hadoop0.20.2+ Nutch1.2+Tomcat7——分布式搜索配置
  • 原文地址:https://www.cnblogs.com/vigorz/p/10499205.html
Copyright © 2011-2022 走看看